<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" dir="ltr"><head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Android 图形用户界面 之 绘图（一） - 蚂蚁乐园 - ITeye技术网站</title>
    <meta name="description" content="直接贴上代码：  &nbsp;  BasicViewDraw.java  &nbsp;  package com.view;    import com.test.R;  import android.view.View;  import android.content.Context;  import android.graphics.Canvas;  import android.graphics.Color;   ...">
    <meta name="keywords" content="Android, 360, ITeye, thread, 游戏 Android 图形用户界面 之 绘图（一）">
    <link rel="shortcut icon" href="http://emmet1988.iteye.com/images/favicon.ico" type="image/x-icon">
    <link rel="search" type="application/opensearchdescription+xml" href="http://emmet1988.iteye.com/open_search.xml" title="ITeye">
    <link href="http://emmet1988.iteye.com/rss" rel="alternate" title="蚂蚁乐园" type="application/rss+xml">
    <link href="1058517_files/blog.css" media="screen" rel="stylesheet" type="text/css">
<link href="1058517_files/blue.css" media="screen" rel="stylesheet" type="text/css">
    <script src="1058517_files/ga.js" async="" type="text/javascript"></script><script src="1058517_files/application.js" type="text/javascript"></script>
    <script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-535605-1']);
  _gaq.push(['_setDomainName', 'iteye.com']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>


      <link href="1058517_files/SyntaxHighlighter.css" media="screen" rel="stylesheet" type="text/css">
  <script src="1058517_files/shCoreCommon.js" type="text/javascript"></script>
<script src="1058517_files/hotkey.js" type="text/javascript"></script>
  <script src="1058517_files/code_favorites.js" type="text/javascript"></script>
<script src="1058517_files/weiboshare.js" type="text/javascript"></script>
  <style type="text/css">.dp-j .annotation { color: #646464; }.dp-j .number { color: #C00000; }</style><style type="text/css">.dp-j .annotation { color: #646464; }.dp-j .number { color: #C00000; }</style><style type="text/css">.dp-j .annotation { color: #646464; }.dp-j .number { color: #C00000; }</style><style type="text/css">.dp-j .annotation { color: #646464; }.dp-j .number { color: #C00000; }</style><style type="text/css">.dp-j .annotation { color: #646464; }.dp-j .number { color: #C00000; }</style><style type="text/css">.dp-j .annotation { color: #646464; }.dp-j .number { color: #C00000; }</style><style type="text/css">.dp-j .annotation { color: #646464; }.dp-j .number { color: #C00000; }</style><style type="text/css">.dp-j .annotation { color: #646464; }.dp-j .number { color: #C00000; }</style><style type="text/css">.dp-j .annotation { color: #646464; }.dp-j .number { color: #C00000; }</style></head>
  <body>
    <div id="header">
      <div id="blog_site_nav">
  <a href="http://www.iteye.com/" class="homepage">首页</a>
  <a href="http://www.iteye.com/news">资讯</a>
  <a href="http://www.iteye.com/magazines">精华</a>
  <a href="http://www.iteye.com/forums">论坛</a>
  <a href="http://www.iteye.com/ask">问答</a>
  <a href="http://www.iteye.com/blogs">博客</a>
  <a href="http://www.iteye.com/blogs/subjects">专栏</a>
  <a href="http://www.iteye.com/groups">群组</a>
  <a href="#" onclick="return false;" id="msna"><u>更多</u> <small>▼</small></a>
  <div class="quick_menu" style="display:none;">
    <a target="_blank" href="http://job.iteye.com/iteye">招聘</a>
    <a href="http://www.iteye.com/search">搜索</a>
  </div>
</div>

      <div id="user_nav">
      <a href="http://emmet1988.iteye.com/login" class="welcome" title="登录">您还未登录 !</a>
    <a href="http://emmet1988.iteye.com/login">登录</a>
    <a href="http://emmet1988.iteye.com/signup" class="nobg">注册</a>
  </div>

    </div>

    <div id="page">
      <div id="branding" class="clearfix">
        <div id="blog_name">
          <h1><a href="http://emmet1988.iteye.com/">蚂蚁乐园</a></h1>
        </div>
        <div id="fd"></div>
        <div id="blog_navbar">
          <ul>
            <li class="blog_navbar_for"><a href="http://emmet1988.iteye.com/"><strong>博客</strong></a></li>
            <li><a href="http://emmet1988.iteye.com/weibo">微博</a></li>
            <li><a href="http://emmet1988.iteye.com/album">相册</a></li>
            <li><a href="http://emmet1988.iteye.com/link">收藏</a></li>
            <li><a href="http://emmet1988.iteye.com/blog/guest_book">留言</a></li>
            <li><a href="http://emmet1988.iteye.com/blog/profile">关于我</a></li>
          </ul>
    
          <div class="search">
            <form action="/blog/search" method="get">
              <input class="search_text" id="query" name="query" style="margin-left: 10px;width: 110px;" type="text">
              <input class="submit_search" value="" type="submit">
            </form>
          </div> 
          <div id="fd"></div>         
        </div>
      </div>
      
      <div id="content" class="clearfix">
        <div id="main">
          



          


<div class="blog_main">
  <div class="blog_title">
    <h3>
      <a href="http://emmet1988.iteye.com/blog/1058517">Android 图形用户界面 之 绘图（一）</a>
      <em class="actions">      </em>
    </h3>
    <ul class="blog_categories"><strong>博客分类：</strong> <li><a href="http://emmet1988.iteye.com/category/156075">Android入门-中级</a></li> </ul>
        <div class="news_tag"><a href="http://www.iteye.com/blogs/tag/Android">Android</a><a href="http://www.iteye.com/blogs/tag/360">360</a><a href="http://www.iteye.com/blogs/tag/ITeye">ITeye</a><a href="http://www.iteye.com/blogs/tag/thread">thread</a><a href="http://www.iteye.com/blogs/tag/%E6%B8%B8%E6%88%8F">游戏</a>&nbsp;</div>
  </div>

  <div id="blog_content" class="blog_content">
    <p>直接贴上代码：</p>
<p>&nbsp;</p>
<p>BasicViewDraw.java</p>
<p>&nbsp;</p>
<div id="" class="dp-highlighter"><div class="bar"><div class="tools">Java代码 <embed wmode="transparent" src="1058517_files/clipboard_new.swf" flashvars="clipboard=package%20com.view%3B%0A%0Aimport%20com.test.R%3B%0Aimport%20android.view.View%3B%0Aimport%20android.content.Context%3B%0Aimport%20android.graphics.Canvas%3B%0Aimport%20android.graphics.Color%3B%0Aimport%20android.graphics.Paint%3B%0Aimport%20android.graphics.RectF%3B%0Aimport%20android.graphics.Path%3B%0Aimport%20android.graphics.Shader%3B%0Aimport%20android.graphics.LinearGradient%3B%0A%0A%2F*%20%E8%87%AA%E5%AE%9A%E4%B9%89%E7%BB%A7%E6%89%BFView%20%E7%9A%84MyView*%2F%0Apublic%20class%20BasicViewDraw%20extends%20View%20%7B%0A%09public%20BasicViewDraw(Context%20context)%20%7B%0A%09%09super(context)%3B%0A%09%7D%0A%0A%09%2F*%20%E9%87%8D%E5%86%99onDraw%EF%BC%88%EF%BC%89%20*%2F%0A%09%40Override%0A%09protected%20void%20onDraw(Canvas%20canvas)%20%7B%0A%09%09super.onDraw(canvas)%3B%0A%09%09%2F*%20%E8%AE%BE%E7%BD%AE%E8%83%8C%E6%99%AF%E4%B8%BA%E7%99%BD%E8%89%B2%20*%2F%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%09%09canvas.drawColor(Color.WHITE)%3B%0A%09%09Paint%20paint%20%3D%20new%20Paint()%3B%0A%09%09%2F*%20%E5%8E%BB%E9%94%AF%E9%BD%BF%20*%2F%0A%09%09paint.setAntiAlias(true)%3B%0A%09%09%2F*%20%E8%AE%BE%E7%BD%AEpaint%E7%9A%84%E9%A2%9C%E8%89%B2%20*%2F%0A%09%09paint.setColor(Color.RED)%3B%0A%09%09%2F*%20%E8%AE%BE%E7%BD%AEpaint%E7%9A%84%20style%20%E4%B8%BASTROKE%EF%BC%9A%E7%A9%BA%E5%BF%83%20*%2F%0A%09%09paint.setStyle(Paint.Style.STROKE)%3B%0A%09%09%2F*%20%E8%AE%BE%E7%BD%AEpaint%E7%9A%84%E5%A4%96%E6%A1%86%E5%AE%BD%E5%BA%A6%20*%2F%0A%09%09paint.setStrokeWidth(3)%3B%0A%09%09%2F*%20%E7%94%BB%E4%B8%80%E4%B8%AA%E7%A9%BA%E5%BF%83%E5%9C%86%E5%BD%A2%20*%2F%0A%09%09canvas.drawCircle(40%2C%2040%2C%2030%2C%20paint)%3B%0A%09%09%2F*%20%E7%94%BB%E4%B8%80%E4%B8%AA%E7%A9%BA%E5%BF%83%E6%AD%A3%E6%96%B9%E5%BD%A2%20*%2F%0A%09%09canvas.drawRect(10%2C%2090%2C%2070%2C%20150%2C%20paint)%3B%0A%09%09%2F*%20%E7%94%BB%E4%B8%80%E4%B8%AA%E7%A9%BA%E5%BF%83%E9%95%BF%E6%96%B9%E5%BD%A2%20*%2F%0A%09%09canvas.drawRect(10%2C%20170%2C%2070%2C%20200%2C%20paint)%3B%0A%09%09%2F*%20%E7%94%BB%E4%B8%80%E4%B8%AA%E7%A9%BA%E5%BF%83%E6%A4%AD%E5%9C%86%E5%BD%A2%20*%2F%0A%09%09RectF%20re%20%3D%20new%20RectF(10%2C%20220%2C%2070%2C%20250)%3B%0A%09%09canvas.drawOval(re%2C%20paint)%3B%0A%09%09%2F*%20%E7%94%BB%E4%B8%80%E4%B8%AA%E7%A9%BA%E5%BF%83%E4%B8%89%E8%A7%92%E5%BD%A2%20*%2F%0A%09%09Path%20path%20%3D%20new%20Path()%3B%0A%09%09path.moveTo(10%2C%20330)%3B%0A%09%09path.lineTo(70%2C%20330)%3B%0A%09%09path.lineTo(40%2C%20270)%3B%0A%09%09path.close()%3B%2F%2F%E8%AE%B0%E5%BE%97%E8%A6%81close%0A%09%09canvas.drawPath(path%2C%20paint)%3B%0A%09%09%2F*%20%E7%94%BB%E4%B8%80%E4%B8%AA%E7%A9%BA%E5%BF%83%E6%A2%AF%E5%BD%A2%20*%2F%0A%09%09Path%20path1%20%3D%20new%20Path()%3B%0A%09%09path1.moveTo(10%2C%20410)%3B%0A%09%09path1.lineTo(70%2C%20410)%3B%0A%09%09path1.lineTo(55%2C%20350)%3B%0A%09%09path1.lineTo(25%2C%20350)%3B%0A%09%09path1.close()%3B%0A%09%09canvas.drawPath(path1%2C%20paint)%3B%0A%0A%09%09%2F*%20%E8%AE%BE%E7%BD%AEpaint%20%E7%9A%84style%E4%B8%BA%20FILL%EF%BC%9A%E5%AE%9E%E5%BF%83%20*%2F%0A%09%09paint.setStyle(Paint.Style.FILL)%3B%0A%09%09%2F*%20%E8%AE%BE%E7%BD%AEpaint%E7%9A%84%E9%A2%9C%E8%89%B2%20*%2F%0A%09%09paint.setColor(Color.BLUE)%3B%0A%09%09%2F*%20%E7%94%BB%E4%B8%80%E4%B8%AA%E5%AE%9E%E5%BF%83%E5%9C%86%20*%2F%0A%09%09canvas.drawCircle(120%2C%2040%2C%2030%2C%20paint)%3B%0A%09%09%2F*%20%E7%94%BB%E4%B8%80%E4%B8%AA%E5%AE%9E%E5%BF%83%E6%AD%A3%E6%96%B9%E5%BD%A2%20*%2F%0A%09%09canvas.drawRect(90%2C%2090%2C%20150%2C%20150%2C%20paint)%3B%0A%09%09%2F*%20%E7%94%BB%E4%B8%80%E4%B8%AA%E5%AE%9E%E5%BF%83%E9%95%BF%E6%96%B9%E5%BD%A2%20*%2F%0A%09%09canvas.drawRect(90%2C%20170%2C%20150%2C%20200%2C%20paint)%3B%0A%09%09%2F*%20%E7%94%BB%E4%B8%80%E4%B8%AA%E5%AE%9E%E5%BF%83%E6%A4%AD%E5%9C%86%20*%2F%0A%09%09RectF%20re2%20%3D%20new%20RectF(90%2C%20220%2C%20150%2C%20250)%3B%0A%09%09canvas.drawOval(re2%2C%20paint)%3B%0A%09%09%2F*%20%E7%94%BB%E4%B8%80%E4%B8%AA%E5%AE%9E%E5%BF%83%E4%B8%89%E8%A7%92%E5%BD%A2%20*%2F%0A%09%09Path%20path2%20%3D%20new%20Path()%3B%0A%09%09path2.moveTo(90%2C%20330)%3B%0A%09%09path2.lineTo(150%2C%20330)%3B%0A%09%09path2.lineTo(120%2C%20270)%3B%0A%09%09path2.close()%3B%0A%09%09canvas.drawPath(path2%2C%20paint)%3B%0A%09%09%2F*%20%E7%94%BB%E4%B8%80%E4%B8%AA%E5%AE%9E%E5%BF%83%E6%A2%AF%E5%BD%A2%20*%2F%0A%09%09Path%20path3%20%3D%20new%20Path()%3B%0A%09%09path3.moveTo(90%2C%20410)%3B%0A%09%09path3.lineTo(150%2C%20410)%3B%0A%09%09path3.lineTo(135%2C%20350)%3B%0A%09%09path3.lineTo(105%2C%20350)%3B%0A%09%09path3.close()%3B%0A%09%09canvas.drawPath(path3%2C%20paint)%3B%0A%09%09%2F*%20%E8%AE%BE%E7%BD%AE%E6%B8%90%E5%8F%98%E8%89%B2%20*%2F%0A%09%09Shader%20mShader%20%3D%20new%20LinearGradient(0%2C%200%2C%20100%2C%20100%2C%20new%20int%5B%5D%20%7B%0A%09%09%09%09Color.RED%2C%20Color.GREEN%2C%20Color.BLUE%2C%20Color.YELLOW%20%7D%2C%20null%2C%0A%09%09%09%09Shader.TileMode.REPEAT)%3B%0A%09%09paint.setShader(mShader)%3B%0A%0A%09%09%2F*%20%E7%94%BB%E4%B8%80%E4%B8%AA%E6%B8%90%E5%8F%98%E8%89%B2%E5%9C%86%20*%2F%0A%09%09canvas.drawCircle(200%2C%2040%2C%2030%2C%20paint)%3B%0A%09%09%2F*%20%E7%94%BB%E4%B8%80%E4%B8%AA%E6%B8%90%E5%8F%98%E8%89%B2%E6%AD%A3%E6%96%B9%E5%BD%A2%20*%2F%0A%09%09canvas.drawRect(170%2C%2090%2C%20230%2C%20150%2C%20paint)%3B%0A%09%09%2F*%20%E7%94%BB%E4%B8%80%E4%B8%AA%E6%B8%90%E5%8F%98%E8%89%B2%E9%95%BF%E6%96%B9%E5%BD%A2%20*%2F%0A%09%09canvas.drawRect(170%2C%20170%2C%20230%2C%20200%2C%20paint)%3B%0A%09%09%2F*%20%E7%94%BB%E4%B8%80%E4%B8%AA%E6%B8%90%E5%8F%98%E8%89%B2%E6%A4%AD%E5%9C%86%20*%2F%0A%09%09RectF%20re3%20%3D%20new%20RectF(170%2C%20220%2C%20230%2C%20250)%3B%0A%09%09canvas.drawOval(re3%2C%20paint)%3B%0A%09%09%2F*%20%E7%94%BB%E4%B8%80%E4%B8%AA%E6%B8%90%E5%8F%98%E8%89%B2%E4%B8%89%E8%A7%92%E5%BD%A2%20*%2F%0A%09%09Path%20path4%20%3D%20new%20Path()%3B%0A%09%09path4.moveTo(170%2C%20330)%3B%0A%09%09path4.lineTo(230%2C%20330)%3B%0A%09%09path4.lineTo(200%2C%20270)%3B%0A%09%09path4.close()%3B%0A%09%09canvas.drawPath(path4%2C%20paint)%3B%0A%09%09%2F*%20%E7%94%BB%E4%B8%80%E4%B8%AA%E6%B8%90%E5%8F%98%E8%89%B2%E6%A2%AF%E5%BD%A2%20*%2F%0A%09%09Path%20path5%20%3D%20new%20Path()%3B%0A%09%09path5.moveTo(170%2C%20410)%3B%0A%09%09path5.lineTo(230%2C%20410)%3B%0A%09%09path5.lineTo(215%2C%20350)%3B%0A%09%09path5.lineTo(185%2C%20350)%3B%0A%09%09path5.close()%3B%0A%09%09canvas.drawPath(path5%2C%20paint)%3B%0A%0A%09%09%2F*%20%E5%86%99%E5%AD%97%20*%2F%0A%09%09paint.setTextSize(24)%3B%0A%09%09canvas.drawText(getResources().getString(R.string.str_text1)%2C%20240%2C%2050%2Cpaint)%3B%0A%09%09canvas.drawText(getResources().getString(R.string.str_text2)%2C%20240%2C%20120%2Cpaint)%3B%0A%09%09canvas.drawText(getResources().getString(R.string.str_text3)%2C%20240%2C%20190%2Cpaint)%3B%0A%09%09canvas.drawText(getResources().getString(R.string.str_text4)%2C%20240%2C%20250%2Cpaint)%3B%0A%09%09canvas.drawText(getResources().getString(R.string.str_text5)%2C%20240%2C%20320%2Cpaint)%3B%0A%09%09canvas.drawText(getResources().getString(R.string.str_text6)%2C%20240%2C%20390%2Cpaint)%3B%0A%09%7D%0A%7D" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="15" width="14">&nbsp;<a href="javascript:void()" title="收藏这段代码" onclick="code_favorites_do_favorite(this);return false;"><img class="star" src="1058517_files/icon_star.png" alt="收藏代码"><img class="spinner" src="1058517_files/spinner.gif" style="display:none"></a></div></div><ol class="dp-j" start="1"><li><span><span class="keyword">package</span><span>&nbsp;com.view;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span><span class="keyword">import</span><span>&nbsp;com.test.R;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.view.View;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.content.Context;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.Canvas;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.Color;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.Paint;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.RectF;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.Path;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.Shader;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.LinearGradient;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span><span class="comment">/*&nbsp;自定义继承View&nbsp;的MyView*/</span><span>&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">public</span><span>&nbsp;</span><span class="keyword">class</span><span>&nbsp;BasicViewDraw&nbsp;</span><span class="keyword">extends</span><span>&nbsp;View&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;BasicViewDraw(Context&nbsp;context)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">super</span><span>(context);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*&nbsp;重写onDraw（）&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="annotation">@Override</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">protected</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;onDraw(Canvas&nbsp;canvas)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">super</span><span>.onDraw(canvas);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*&nbsp;设置背景为白色&nbsp;*/</span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawColor(Color.WHITE);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Paint&nbsp;paint&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Paint();&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*&nbsp;去锯齿&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setAntiAlias(<span class="keyword">true</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*&nbsp;设置paint的颜色&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setColor(Color.RED);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*&nbsp;设置paint的&nbsp;style&nbsp;为STROKE：空心&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setStyle(Paint.Style.STROKE);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*&nbsp;设置paint的外框宽度&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setStrokeWidth(<span class="number">3</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*&nbsp;画一个空心圆形&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawCircle(<span class="number">40</span><span>,&nbsp;</span><span class="number">40</span><span>,&nbsp;</span><span class="number">30</span><span>,&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*&nbsp;画一个空心正方形&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawRect(<span class="number">10</span><span>,&nbsp;</span><span class="number">90</span><span>,&nbsp;</span><span class="number">70</span><span>,&nbsp;</span><span class="number">150</span><span>,&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*&nbsp;画一个空心长方形&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawRect(<span class="number">10</span><span>,&nbsp;</span><span class="number">170</span><span>,&nbsp;</span><span class="number">70</span><span>,&nbsp;</span><span class="number">200</span><span>,&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*&nbsp;画一个空心椭圆形&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RectF&nbsp;re&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;RectF(</span><span class="number">10</span><span>,&nbsp;</span><span class="number">220</span><span>,&nbsp;</span><span class="number">70</span><span>,&nbsp;</span><span class="number">250</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawOval(re,&nbsp;paint);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*&nbsp;画一个空心三角形&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Path&nbsp;path&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Path();&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path.moveTo(<span class="number">10</span><span>,&nbsp;</span><span class="number">330</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path.lineTo(<span class="number">70</span><span>,&nbsp;</span><span class="number">330</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path.lineTo(<span class="number">40</span><span>,&nbsp;</span><span class="number">270</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path.close();<span class="comment">//记得要close</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawPath(path,&nbsp;paint);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*&nbsp;画一个空心梯形&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Path&nbsp;path1&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Path();&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path1.moveTo(<span class="number">10</span><span>,&nbsp;</span><span class="number">410</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path1.lineTo(<span class="number">70</span><span>,&nbsp;</span><span class="number">410</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path1.lineTo(<span class="number">55</span><span>,&nbsp;</span><span class="number">350</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path1.lineTo(<span class="number">25</span><span>,&nbsp;</span><span class="number">350</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path1.close();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawPath(path1,&nbsp;paint);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*&nbsp;设置paint&nbsp;的style为&nbsp;FILL：实心&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setStyle(Paint.Style.FILL);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*&nbsp;设置paint的颜色&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setColor(Color.BLUE);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*&nbsp;画一个实心圆&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawCircle(<span class="number">120</span><span>,&nbsp;</span><span class="number">40</span><span>,&nbsp;</span><span class="number">30</span><span>,&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*&nbsp;画一个实心正方形&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawRect(<span class="number">90</span><span>,&nbsp;</span><span class="number">90</span><span>,&nbsp;</span><span class="number">150</span><span>,&nbsp;</span><span class="number">150</span><span>,&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*&nbsp;画一个实心长方形&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawRect(<span class="number">90</span><span>,&nbsp;</span><span class="number">170</span><span>,&nbsp;</span><span class="number">150</span><span>,&nbsp;</span><span class="number">200</span><span>,&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*&nbsp;画一个实心椭圆&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RectF&nbsp;re2&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;RectF(</span><span class="number">90</span><span>,&nbsp;</span><span class="number">220</span><span>,&nbsp;</span><span class="number">150</span><span>,&nbsp;</span><span class="number">250</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawOval(re2,&nbsp;paint);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*&nbsp;画一个实心三角形&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Path&nbsp;path2&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Path();&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path2.moveTo(<span class="number">90</span><span>,&nbsp;</span><span class="number">330</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path2.lineTo(<span class="number">150</span><span>,&nbsp;</span><span class="number">330</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path2.lineTo(<span class="number">120</span><span>,&nbsp;</span><span class="number">270</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path2.close();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawPath(path2,&nbsp;paint);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*&nbsp;画一个实心梯形&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Path&nbsp;path3&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Path();&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path3.moveTo(<span class="number">90</span><span>,&nbsp;</span><span class="number">410</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path3.lineTo(<span class="number">150</span><span>,&nbsp;</span><span class="number">410</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path3.lineTo(<span class="number">135</span><span>,&nbsp;</span><span class="number">350</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path3.lineTo(<span class="number">105</span><span>,&nbsp;</span><span class="number">350</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path3.close();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawPath(path3,&nbsp;paint);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*&nbsp;设置渐变色&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Shader&nbsp;mShader&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;LinearGradient(</span><span class="number">0</span><span>,&nbsp;</span><span class="number">0</span><span>,&nbsp;</span><span class="number">100</span><span>,&nbsp;</span><span class="number">100</span><span>,&nbsp;</span><span class="keyword">new</span><span>&nbsp;</span><span class="keyword">int</span><span>[]&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Color.RED,&nbsp;Color.GREEN,&nbsp;Color.BLUE,&nbsp;Color.YELLOW&nbsp;},&nbsp;<span class="keyword">null</span><span>,&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Shader.TileMode.REPEAT);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setShader(mShader);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*&nbsp;画一个渐变色圆&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawCircle(<span class="number">200</span><span>,&nbsp;</span><span class="number">40</span><span>,&nbsp;</span><span class="number">30</span><span>,&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*&nbsp;画一个渐变色正方形&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawRect(<span class="number">170</span><span>,&nbsp;</span><span class="number">90</span><span>,&nbsp;</span><span class="number">230</span><span>,&nbsp;</span><span class="number">150</span><span>,&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*&nbsp;画一个渐变色长方形&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawRect(<span class="number">170</span><span>,&nbsp;</span><span class="number">170</span><span>,&nbsp;</span><span class="number">230</span><span>,&nbsp;</span><span class="number">200</span><span>,&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*&nbsp;画一个渐变色椭圆&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RectF&nbsp;re3&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;RectF(</span><span class="number">170</span><span>,&nbsp;</span><span class="number">220</span><span>,&nbsp;</span><span class="number">230</span><span>,&nbsp;</span><span class="number">250</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawOval(re3,&nbsp;paint);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*&nbsp;画一个渐变色三角形&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Path&nbsp;path4&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Path();&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path4.moveTo(<span class="number">170</span><span>,&nbsp;</span><span class="number">330</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path4.lineTo(<span class="number">230</span><span>,&nbsp;</span><span class="number">330</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path4.lineTo(<span class="number">200</span><span>,&nbsp;</span><span class="number">270</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path4.close();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawPath(path4,&nbsp;paint);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*&nbsp;画一个渐变色梯形&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Path&nbsp;path5&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Path();&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path5.moveTo(<span class="number">170</span><span>,&nbsp;</span><span class="number">410</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path5.lineTo(<span class="number">230</span><span>,&nbsp;</span><span class="number">410</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path5.lineTo(<span class="number">215</span><span>,&nbsp;</span><span class="number">350</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path5.lineTo(<span class="number">185</span><span>,&nbsp;</span><span class="number">350</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path5.close();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawPath(path5,&nbsp;paint);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*&nbsp;写字&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setTextSize(<span class="number">24</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawText(getResources().getString(R.string.str_text1),&nbsp;<span class="number">240</span><span>,&nbsp;</span><span class="number">50</span><span>,paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawText(getResources().getString(R.string.str_text2),&nbsp;<span class="number">240</span><span>,&nbsp;</span><span class="number">120</span><span>,paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawText(getResources().getString(R.string.str_text3),&nbsp;<span class="number">240</span><span>,&nbsp;</span><span class="number">190</span><span>,paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawText(getResources().getString(R.string.str_text4),&nbsp;<span class="number">240</span><span>,&nbsp;</span><span class="number">250</span><span>,paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawText(getResources().getString(R.string.str_text5),&nbsp;<span class="number">240</span><span>,&nbsp;</span><span class="number">320</span><span>,paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawText(getResources().getString(R.string.str_text6),&nbsp;<span class="number">240</span><span>,&nbsp;</span><span class="number">390</span><span>,paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>}&nbsp;&nbsp;</span></li></ol></div><pre title="Android 图形用户界面 之 绘图（一）" pre_index="0" source_url="http://emmet1988.iteye.com/blog/1058517" codeable_type="Blog" codeable_id="1058517" style="display: none;" class="java" name="code">package com.view;

import com.test.R;
import android.view.View;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Path;
import android.graphics.Shader;
import android.graphics.LinearGradient;

/* 自定义继承View 的MyView*/
public class BasicViewDraw extends View {
	public BasicViewDraw(Context context) {
		super(context);
	}

	/* 重写onDraw（） */
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		/* 设置背景为白色 */                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
		canvas.drawColor(Color.WHITE);
		Paint paint = new Paint();
		/* 去锯齿 */
		paint.setAntiAlias(true);
		/* 设置paint的颜色 */
		paint.setColor(Color.RED);
		/* 设置paint的 style 为STROKE：空心 */
		paint.setStyle(Paint.Style.STROKE);
		/* 设置paint的外框宽度 */
		paint.setStrokeWidth(3);
		/* 画一个空心圆形 */
		canvas.drawCircle(40, 40, 30, paint);
		/* 画一个空心正方形 */
		canvas.drawRect(10, 90, 70, 150, paint);
		/* 画一个空心长方形 */
		canvas.drawRect(10, 170, 70, 200, paint);
		/* 画一个空心椭圆形 */
		RectF re = new RectF(10, 220, 70, 250);
		canvas.drawOval(re, paint);
		/* 画一个空心三角形 */
		Path path = new Path();
		path.moveTo(10, 330);
		path.lineTo(70, 330);
		path.lineTo(40, 270);
		path.close();//记得要close
		canvas.drawPath(path, paint);
		/* 画一个空心梯形 */
		Path path1 = new Path();
		path1.moveTo(10, 410);
		path1.lineTo(70, 410);
		path1.lineTo(55, 350);
		path1.lineTo(25, 350);
		path1.close();
		canvas.drawPath(path1, paint);

		/* 设置paint 的style为 FILL：实心 */
		paint.setStyle(Paint.Style.FILL);
		/* 设置paint的颜色 */
		paint.setColor(Color.BLUE);
		/* 画一个实心圆 */
		canvas.drawCircle(120, 40, 30, paint);
		/* 画一个实心正方形 */
		canvas.drawRect(90, 90, 150, 150, paint);
		/* 画一个实心长方形 */
		canvas.drawRect(90, 170, 150, 200, paint);
		/* 画一个实心椭圆 */
		RectF re2 = new RectF(90, 220, 150, 250);
		canvas.drawOval(re2, paint);
		/* 画一个实心三角形 */
		Path path2 = new Path();
		path2.moveTo(90, 330);
		path2.lineTo(150, 330);
		path2.lineTo(120, 270);
		path2.close();
		canvas.drawPath(path2, paint);
		/* 画一个实心梯形 */
		Path path3 = new Path();
		path3.moveTo(90, 410);
		path3.lineTo(150, 410);
		path3.lineTo(135, 350);
		path3.lineTo(105, 350);
		path3.close();
		canvas.drawPath(path3, paint);
		/* 设置渐变色 */
		Shader mShader = new LinearGradient(0, 0, 100, 100, new int[] {
				Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW }, null,
				Shader.TileMode.REPEAT);
		paint.setShader(mShader);

		/* 画一个渐变色圆 */
		canvas.drawCircle(200, 40, 30, paint);
		/* 画一个渐变色正方形 */
		canvas.drawRect(170, 90, 230, 150, paint);
		/* 画一个渐变色长方形 */
		canvas.drawRect(170, 170, 230, 200, paint);
		/* 画一个渐变色椭圆 */
		RectF re3 = new RectF(170, 220, 230, 250);
		canvas.drawOval(re3, paint);
		/* 画一个渐变色三角形 */
		Path path4 = new Path();
		path4.moveTo(170, 330);
		path4.lineTo(230, 330);
		path4.lineTo(200, 270);
		path4.close();
		canvas.drawPath(path4, paint);
		/* 画一个渐变色梯形 */
		Path path5 = new Path();
		path5.moveTo(170, 410);
		path5.lineTo(230, 410);
		path5.lineTo(215, 350);
		path5.lineTo(185, 350);
		path5.close();
		canvas.drawPath(path5, paint);

		/* 写字 */
		paint.setTextSize(24);
		canvas.drawText(getResources().getString(R.string.str_text1), 240, 50,paint);
		canvas.drawText(getResources().getString(R.string.str_text2), 240, 120,paint);
		canvas.drawText(getResources().getString(R.string.str_text3), 240, 190,paint);
		canvas.drawText(getResources().getString(R.string.str_text4), 240, 250,paint);
		canvas.drawText(getResources().getString(R.string.str_text5), 240, 320,paint);
		canvas.drawText(getResources().getString(R.string.str_text6), 240, 390,paint);
	}
}</pre>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>BasicView2Draw.java</p>
<p>&nbsp;</p>
<div id="" class="dp-highlighter"><div class="bar"><div class="tools">Java代码 <embed wmode="transparent" src="1058517_files/clipboard_new.swf" flashvars="clipboard=class%20BasicView2Draw%20extends%20View%7B%0A%09%09%0A%09%09Paint%20paint%3B%0A%09%09Bitmap%20bitmap%3B%0A%09%09%0A%09%09public%20BasicView2Draw(Context%20context)%20%7B%0A%09%09%09super(context)%3B%0A%09%09%09paint%20%3D%20new%20Paint(Paint.ANTI_ALIAS_FLAG)%3B%0A%09%09%09bitmap%20%3D%20BitmapFactory.decodeResource(getResources()%2C%20R.drawable.icon)%3B%0A%09%09%7D%0A%09%09%0A%09%09private%20Bitmap%20createBitmap1()%7B%0A%09%09%09Bitmap%20bitmap1%20%3D%20Bitmap.createBitmap(100%2C%20100%2C%20Config.ARGB_8888)%3B%0A%09%09%09Canvas%20canvas%20%3D%20new%20Canvas(bitmap1)%3B%0A%09%09%09canvas.drawColor(Color.BLUE)%3B%0A%2F%2F%09%20%20%09%09canvas.drawARGB(0%2C%200%2C%200%2C%200)%3B%2F%2F%20%E9%80%8F%E6%98%8E%E8%89%B2%0A%09%09%09canvas.drawBitmap(bitmap%2C%200%2C%200%2C%20paint)%3B%0A%09%09%09canvas.drawText(%22Hello%20Android%22%2C%2025%2C%2055%2C%20paint)%3B%0A%09%09%09return%20bitmap1%3B%0A%09%09%7D%0A%09%09%0A%09%09%40Override%0A%09%09protected%20void%20onDraw(Canvas%20canvas)%20%7B%0A%09%09%09super.onDraw(canvas)%3B%0A%09%09%09%0A%09%09%09%2F%2F%20%E7%BB%98%E5%88%B6%E4%BD%8D%E5%9B%BE%0A%09%09%09%2F%2F%201.%E7%BB%98%E5%88%B6%E4%BD%8D%E5%9B%BE%E5%9C%A8%EF%BC%8810%EF%BC%8C10%EF%BC%89%E4%BD%8D%E7%BD%AE%E4%B8%8A%0A%09%09%09canvas.drawBitmap(createBitmap1()%2C%2010%2C%2010%2C%20paint)%3B%0A%09%09%09%2F%2F%202.%20%09canvas.drawBitmap(Bitmap%20bitmap%2CRect%20src%2CRect%20dest%2CPaint%20paint)%3B%0A%09%09%09%2F%2F%20%09%09canvas.drawBitmap(Bitmap%20bitmap%2CRect%20src%2CRectF%20dest%2CPaint%20paint)%3B%0A%09%09%09%2F%2F%20%09%E7%BB%98%E5%88%B6%E4%BD%8D%E5%9B%BE%E5%88%B0%E4%B8%80%E4%B8%AA%E6%8C%87%E5%AE%9A%E7%9A%84%E7%9F%A9%E5%BD%A2dest%E4%B8%AD%EF%BC%8C%E4%BD%8D%E5%9B%BE%E4%BC%9A%E8%87%AA%E5%8A%A8%E8%BF%9B%E8%A1%8C%E5%B9%B3%E7%A7%BB%E5%92%8C%E7%BC%A9%E6%94%BE%E7%AD%89%E6%93%8D%E4%BD%9C%EF%BC%8C%E5%A6%82%E6%9E%9Csrc%E7%9A%84%E5%8F%82%E6%95%B0%E4%B8%8D%E4%B8%BAnull%0A%09%09%09%2F%2F%20%E5%88%99%E4%BC%9A%E8%A3%81%E5%89%AA%E4%BD%8D%E5%9B%BE%E7%9A%84%E9%83%A8%E5%88%86%E5%8C%BA%E5%9F%9F%E6%9D%A5%E8%BF%9B%E8%A1%8C%E7%BB%98%E5%88%B6%0A%09%09%09Rect%20rect%20%3D%20new%20Rect(10%2C%2010%2C%2050%2C%2060)%3B%0A%09%09%09RectF%20rectF1%20%3D%20new%20RectF(180.0f%2C%2020.0f%2C%20240.0f%2C%2080.0f)%3B%0A%09%09%09RectF%20rectF2%20%3D%20new%20RectF(180.0f%2C%20100.0f%2C%20240.0f%2C%20160.0f)%3B%0A%09%09%09canvas.drawBitmap(createBitmap1()%2C%20null%2C%20rectF1%2C%20paint)%3B%0A%09%09%09canvas.drawBitmap(createBitmap1()%2C%20rect%2C%20rectF2%2C%20paint)%3B%0A%09%09%09%0A%09%09%09%2F%2F%20%E7%82%B9%0A%09%09%09paint.setStyle(Paint.Style.FILL_AND_STROKE)%3B%0A%09%09%09paint.setStrokeWidth(5.0f)%3B%0A%09%09%09paint.setColor(Color.YELLOW)%3B%0A%09%09%09canvas.drawPoints(new%20float%5B%5D%7B120%2C120%2C140%2C140%2C160%2C160%2C180%2C180%7D%2C%20paint)%3B%0A%09%09%09%0A%09%09%09%2F%2F%20%E7%BA%BF%0A%09%09%09paint.reset()%3B%2F%2F%20%E9%87%8D%E7%BD%AE%E7%94%BB%E7%AC%94%0A%09%09%09paint.setColor(Color.GREEN)%3B%0A%09%09%09paint.setAntiAlias(true)%3B%0A%09%09%09canvas.drawLine(30%2C%2030%2C%20130%2C%2040%2C%20paint)%3B%0A%09%09%09paint.setColor(Color.RED)%3B%0A%09%09%09canvas.drawLines(new%20float%5B%5D%7B%20%2040%2C40%2C140%2C40%20%20%2C50%2C50%2C90%2C90%20%20%7D%2C%20paint)%3B%0A%09%09%09%0A%09%09%09%2F%2F%20%E7%9F%A9%E5%BD%A2%0A%09%09%09paint.setColor(Color.CYAN)%3B%0A%09%09%09canvas.drawRect(10%2C%20150%2C%20150%2C%20250%2C%20paint)%3B%0A%09%09%09paint.setColor(Color.GRAY)%3B%0A%09%09%09canvas.drawRect(new%20Rect(10%2C%20260%2C%20150%2C%20280)%2C%20paint)%3B%0A%09%09%09paint.setColor(Color.DKGRAY)%3B%0A%09%09%09canvas.drawRect(new%20RectF(20.2f%2C%20290.9f%2C%20120.2f%2C%20300.3f)%2C%20paint)%3B%0A%09%09%09%0A%09%09%09%2F%2F%20%E7%BB%98%E5%88%B6%E6%96%87%E6%9C%AC%0A%2F%2F%09%09%09paint.setTextSize(20)%3B%0A%2F%2F%09%09%09paint.setColor(0x40ffffff)%3B%2F%2F%20%E5%8D%8A%E9%80%8F%E6%98%8E%E7%99%BD%E8%89%B2%0A%2F%2F%09%09%09paint.setTextAlign(Paint.Align.RIGHT)%3B%2F%2F%20%E5%AF%B9%E9%BD%90%E6%96%B9%E5%90%91%0A%2F%2F%09%09%09canvas.drawText(%22Cool%20Android%22%2C%20250%2C%20180%2C%20paint)%3B%2F%2F%20%E8%BF%99%E9%87%8C%E6%B3%A8%E6%84%8F%EF%BC%8C%E5%9D%90%E6%A0%87(180%2C180)%E6%98%AF%E6%96%87%E6%9C%AC%E7%9A%84%E5%B7%A6%E4%B8%8B%E7%82%B9%E5%9D%90%E6%A0%87%0A%09%09%09%0A%09%09%09%2F%2F%20%E7%94%BB%E5%B8%83%E5%B9%B3%E7%A7%BB%EF%BC%9A%0A%09%09%09%2F%2F%20%E5%B9%B3%E7%A7%BB%E7%9A%84%E5%8D%95%E4%BD%8D%E6%98%AF%E5%83%8F%E7%B4%A0%EF%BC%8C%E5%88%86%E5%88%AB%E6%98%AF%E5%9C%A8x%2Cy%E8%BD%B4%E4%B8%8A%E5%B9%B3%E7%A7%BB%E7%9A%84%E5%83%8F%E7%B4%A0%E7%82%B9%0A%09%09%09%2F%2F%20%E6%AD%A3%E6%95%B0%E4%BB%A3%E8%A1%A8%E7%9A%84%E6%AD%A3%E6%96%B9%E5%90%91%EF%BC%8Cx%E8%BD%B4%E4%B8%BA%E5%B9%B3%E9%9D%A2%E7%9A%84%E5%8F%B3%E4%BE%A7%EF%BC%8Cy%E8%BD%B4%E4%B8%BA%E5%B9%B3%E9%9D%A2%E7%9A%84%E4%B8%8B%E6%96%B9%EF%BC%8C%E7%9B%B8%E5%BA%94%E7%9A%84%EF%BC%8C%E8%B4%9F%E6%95%B0%E5%88%99%E5%90%91%E5%8F%8D%E6%96%B9%E5%90%91%E5%B9%B3%E7%A7%BB%0A%2F%2F%09%09%09canvas.translate(30.0f%2C%2030.0f)%3B%0A%09%09%09%0A%09%09%09%2F%2F%20%E7%94%BB%E5%B8%83%E7%BC%A9%E6%94%BE%EF%BC%9A%0A%09%09%09%2F%2F%20%E5%8F%82%E6%95%B0%E5%88%86%E5%88%AB%E6%98%AF%E5%9C%A8%E6%83%B3x%EF%BC%8Cy%E8%BD%B4%E4%B8%8A%E6%94%BE%E5%A4%A7%E6%88%96%E7%BC%A9%E5%B0%8F%E7%9A%84%E5%80%8D%E6%95%B0%EF%BC%8C%E5%A4%A7%E9%9B%A81%E4%B8%BA%E6%94%BE%E5%A4%A7%EF%BC%8C%E5%B0%8F%E4%BA%8E1%E4%B8%BA%E7%BC%A9%E5%B0%8F%EF%BC%8C%0A%09%09%09%2F%2F%20%E7%BC%A9%E6%94%BE%E7%9A%84%E5%8E%9F%E7%82%B9%E9%BB%98%E8%AE%A4%E4%B8%BA%E7%94%BB%E5%B8%83%E7%9A%84%E5%8E%9F%E7%82%B9(0%2C0)%EF%BC%8C%E4%B9%9F%E5%8F%AF%E4%BB%A5%E6%8C%87%E5%AE%9A%E7%BC%A9%E6%94%BE%E7%9A%84%E5%8E%9F%E7%82%B9%0A%2F%2F%09%09%09canvas.scale(2.0f%2C%201.5f)%3B%0A%2F%2F%09%09%09canvas.scale(0.5f%2C%200.5f%2C%20100.0f%2C%20100.0f)%3B%2F%2F%20%E6%8C%87%E5%AE%9A%E5%9D%90%E6%A0%87(100.0f%2C100.0f)%E4%B8%BA%E7%BC%A9%E6%94%BE%E5%8E%9F%E7%82%B9%0A%09%09%09%2F%2F%20%E8%BF%99%E9%87%8C%E5%89%96%E6%9E%90%E4%B8%80%E4%B8%8B%E7%AC%AC%E4%BA%8C%E4%B8%AA%E7%BC%A9%E6%94%BE%E6%96%B9%E6%B3%95%EF%BC%8C%E5%85%B6%E5%AE%9E%E7%B3%BB%E7%BB%9F%E4%B8%BA%E6%88%91%E4%BB%AC%E5%81%9A%E7%9A%84%E4%BA%8B%E6%83%85%E6%98%AF%E8%BF%99%E6%A0%B7%E7%9A%84%0A%09%09%09%2F*%0A%09%09%09%20%20%20scale(float%20sx%2C%20float%20sy%2C%20float%20px%2C%20float%20py)%7B%0A%09%09%09%09%09translate(px%2Cpy)%3B%0A%09%09%09%09%09scale(sx%2Csy)%3B%0A%09%09%09%09%09translate(-px%2C-py)%3B%0A%09%09%09%09%7D%0A%09%09%09*%2F%0A%09%09%09%0A%09%09%09%2F%2F%20%E7%94%BB%E5%B8%83%E6%97%8B%E8%BD%AC%0A%09%09%09%2F%2F%201.%E4%BB%A5%E7%94%BB%E5%B8%83%E4%B8%BA%E5%8E%9F%E7%82%B9%EF%BC%8C%E9%A1%BA%E6%97%B6%E9%92%88%E6%97%8B%E8%BD%AC40.0f%E5%BA%A6%0A%2F%2F%09%09%09canvas.rotate(40.0f)%3B%0A%09%09%09%2F%2F%202.%E4%BB%A5(100.11f%2C%20100.22f)%E4%B8%BA%E5%8E%9F%E7%82%B9%EF%BC%8C%E9%A1%BA%E6%97%B6%E9%92%88%E6%97%8B%E8%BD%AC50.0f%E5%BA%A6%0A%2F%2F%09%09%09canvas.rotate(50.0f%2C%20100.11f%2C%20100.22f)%3B%0A%09%09%09%2F%2F%20%E7%9B%B8%E5%BA%94%E7%9A%84%EF%BC%8C%E4%B8%BA%E4%BA%86%E5%8A%A0%E6%B7%B1%E7%90%86%E8%A7%A3%EF%BC%8C%E6%88%91%E4%BB%AC%E5%86%8D%E5%89%96%E6%9E%90%E4%B8%80%E4%B8%8B%E7%AC%AC%E4%BA%8C%E4%B8%AA%E6%97%8B%E8%BD%AC%E6%96%B9%E6%B3%95%0A%09%09%09%2F%2F%20%EF%BC%8C%E5%85%B6%E5%AE%9E%E7%B3%BB%E7%BB%9F%E4%B8%BA%E6%88%91%E4%BB%AC%E5%81%9A%E7%9A%84%E4%BA%8B%E6%83%85%E6%98%AF%E8%BF%99%E6%A0%B7%E7%9A%84%0A%09%09%09%2F*%0A%09%09%09%20%20%20rotate(float%20degrees%2C%20float%20px%2C%20float%20py)%7B%0A%09%09%09%09%09translate(px%2Cpy)%3B%0A%09%09%09%09%09rotate(degrees)%3B%0A%09%09%09%09%09translate(-px%2C-py)%3B%0A%09%09%09%09%7D%0A%09%09%09*%2F%0A%09%09%09%0A%09%09%09%2F%2F%20%E7%94%BB%E5%B8%83%E5%80%BE%E6%96%9C%0A%09%09%09%2F%2F%20skew(float%20sx%2Cfloat%20xy)%3B%E5%B0%86%E7%94%BB%E5%B8%83%E5%9C%A8x%E5%8F%8Ay%E8%BD%B4%E6%96%B9%E5%90%91%E4%B8%8A%E5%80%BE%E6%96%9C%E7%9B%B8%E5%BA%94%E7%9A%84%E8%A7%92%E5%BA%A6%EF%BC%8Csx%E6%88%96sy%E4%B8%BA%E5%80%BE%E6%96%9C%E8%A7%92%E5%BA%A6%E7%9A%84tan%E5%80%BC%EF%BC%8C%0A%09%09%09%2F%2F%20%E5%A6%82canvas.skew(1%2C0)%3B%E4%B8%BA%E5%9C%A8x%E6%96%B9%E5%90%91%E4%B8%8A%E5%80%BE%E6%96%9C45%E5%BA%A6%20%3E%3E%20tan(45)%20%3D%201%0A%2F%2F%09%09%09canvas.skew(1%2C0)%3B%0A%09%09%7D%0A%09%7D" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="15" width="14">&nbsp;<a href="javascript:void()" title="收藏这段代码" onclick="code_favorites_do_favorite(this);return false;"><img class="star" src="1058517_files/icon_star.png" alt="收藏代码"><img class="spinner" src="1058517_files/spinner.gif" style="display:none"></a></div></div><ol class="dp-j" start="1"><li><span><span class="keyword">class</span><span>&nbsp;BasicView2Draw&nbsp;</span><span class="keyword">extends</span><span>&nbsp;View{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Paint&nbsp;paint;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Bitmap&nbsp;bitmap;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;BasicView2Draw(Context&nbsp;context)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">super</span><span>(context);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Paint(Paint.ANTI_ALIAS_FLAG);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bitmap&nbsp;=&nbsp;BitmapFactory.decodeResource(getResources(),&nbsp;R.drawable.icon);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">private</span><span>&nbsp;Bitmap&nbsp;createBitmap1(){&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Bitmap&nbsp;bitmap1&nbsp;=&nbsp;Bitmap.createBitmap(<span class="number">100</span><span>,&nbsp;</span><span class="number">100</span><span>,&nbsp;Config.ARGB_8888);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Canvas&nbsp;canvas&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Canvas(bitmap1);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawColor(Color.BLUE);&nbsp;&nbsp;</span></li><li><span><span class="comment">//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawARGB(0,&nbsp;0,&nbsp;0,&nbsp;0);//&nbsp;透明色</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawBitmap(bitmap,&nbsp;<span class="number">0</span><span>,&nbsp;</span><span class="number">0</span><span>,&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawText(<span class="string">"Hello&nbsp;Android"</span><span>,&nbsp;</span><span class="number">25</span><span>,&nbsp;</span><span class="number">55</span><span>,&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">return</span><span>&nbsp;bitmap1;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="annotation">@Override</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">protected</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;onDraw(Canvas&nbsp;canvas)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">super</span><span>.onDraw(canvas);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;绘制位图</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;1.绘制位图在（10，10）位置上</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawBitmap(createBitmap1(),&nbsp;<span class="number">10</span><span>,&nbsp;</span><span class="number">10</span><span>,&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;2.&nbsp;&nbsp;&nbsp;canvas.drawBitmap(Bitmap&nbsp;bitmap,Rect&nbsp;src,Rect&nbsp;dest,Paint&nbsp;paint);</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawBitmap(Bitmap&nbsp;bitmap,Rect&nbsp;src,RectF&nbsp;dest,Paint&nbsp;paint);</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;&nbsp;绘制位图到一个指定的矩形dest中，位图会自动进行平移和缩放等操作，如果src的参数不为null</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;则会裁剪位图的部分区域来进行绘制</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Rect&nbsp;rect&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Rect(</span><span class="number">10</span><span>,&nbsp;</span><span class="number">10</span><span>,&nbsp;</span><span class="number">50</span><span>,&nbsp;</span><span class="number">60</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RectF&nbsp;rectF1&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;RectF(</span><span class="number">180</span><span>.0f,&nbsp;</span><span class="number">20</span><span>.0f,&nbsp;</span><span class="number">240</span><span>.0f,&nbsp;</span><span class="number">80</span><span>.0f);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RectF&nbsp;rectF2&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;RectF(</span><span class="number">180</span><span>.0f,&nbsp;</span><span class="number">100</span><span>.0f,&nbsp;</span><span class="number">240</span><span>.0f,&nbsp;</span><span class="number">160</span><span>.0f);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawBitmap(createBitmap1(),&nbsp;<span class="keyword">null</span><span>,&nbsp;rectF1,&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawBitmap(createBitmap1(),&nbsp;rect,&nbsp;rectF2,&nbsp;paint);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;点</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setStyle(Paint.Style.FILL_AND_STROKE);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setStrokeWidth(<span class="number">5</span><span>.0f);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setColor(Color.YELLOW);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawPoints(<span class="keyword">new</span><span>&nbsp;</span><span class="keyword">float</span><span>[]{</span><span class="number">120</span><span>,</span><span class="number">120</span><span>,</span><span class="number">140</span><span>,</span><span class="number">140</span><span>,</span><span class="number">160</span><span>,</span><span class="number">160</span><span>,</span><span class="number">180</span><span>,</span><span class="number">180</span><span>},&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;线</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.reset();<span class="comment">//&nbsp;重置画笔</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setColor(Color.GREEN);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setAntiAlias(<span class="keyword">true</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawLine(<span class="number">30</span><span>,&nbsp;</span><span class="number">30</span><span>,&nbsp;</span><span class="number">130</span><span>,&nbsp;</span><span class="number">40</span><span>,&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setColor(Color.RED);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawLines(<span class="keyword">new</span><span>&nbsp;</span><span class="keyword">float</span><span>[]{&nbsp;&nbsp;</span><span class="number">40</span><span>,</span><span class="number">40</span><span>,</span><span class="number">140</span><span>,</span><span class="number">40</span><span>&nbsp;&nbsp;,</span><span class="number">50</span><span>,</span><span class="number">50</span><span>,</span><span class="number">90</span><span>,</span><span class="number">90</span><span>&nbsp;&nbsp;},&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;矩形</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setColor(Color.CYAN);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawRect(<span class="number">10</span><span>,&nbsp;</span><span class="number">150</span><span>,&nbsp;</span><span class="number">150</span><span>,&nbsp;</span><span class="number">250</span><span>,&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setColor(Color.GRAY);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawRect(<span class="keyword">new</span><span>&nbsp;Rect(</span><span class="number">10</span><span>,&nbsp;</span><span class="number">260</span><span>,&nbsp;</span><span class="number">150</span><span>,&nbsp;</span><span class="number">280</span><span>),&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setColor(Color.DKGRAY);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawRect(<span class="keyword">new</span><span>&nbsp;RectF(</span><span class="number">20</span><span>.2f,&nbsp;</span><span class="number">290</span><span>.9f,&nbsp;</span><span class="number">120</span><span>.2f,&nbsp;</span><span class="number">300</span><span>.3f),&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;绘制文本</span><span>&nbsp;&nbsp;</span></span></li><li><span><span class="comment">//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setTextSize(20);</span><span>&nbsp;&nbsp;</span></span></li><li><span><span class="comment">//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setColor(0x40ffffff);//&nbsp;半透明白色</span><span>&nbsp;&nbsp;</span></span></li><li><span><span class="comment">//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setTextAlign(Paint.Align.RIGHT);//&nbsp;对齐方向</span><span>&nbsp;&nbsp;</span></span></li><li><span><span class="comment">//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawText("Cool&nbsp;Android",&nbsp;250,&nbsp;180,&nbsp;paint);
//&nbsp;这里注意，坐标(180,180)是文本的左下点坐标</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;画布平移：</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;平移的单位是像素，分别是在x,y轴上平移的像素点</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;正数代表的正方向，x轴为平面的右侧，y轴为平面的下方，相应的，负数则向反方向平移</span><span>&nbsp;&nbsp;</span></span></li><li><span><span class="comment">//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.translate(30.0f,&nbsp;30.0f);</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;画布缩放：</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;参数分别是在想x，y轴上放大或缩小的倍数，大雨1为放大，小于1为缩小，</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;缩放的原点默认为画布的原点(0,0)，也可以指定缩放的原点</span><span>&nbsp;&nbsp;</span></span></li><li><span><span class="comment">//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.scale(2.0f,&nbsp;1.5f);</span><span>&nbsp;&nbsp;</span></span></li><li><span><span class="comment">//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.scale(0.5f,&nbsp;0.5f,&nbsp;100.0f,&nbsp;100.0f);
//&nbsp;指定坐标(100.0f,100.0f)为缩放原点</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;这里剖析一下第二个缩放方法，其实系统为我们做的事情是这样的</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scale(float&nbsp;sx,&nbsp;float&nbsp;sy,&nbsp;float&nbsp;px,&nbsp;float&nbsp;py){</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;translate(px,py);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scale(sx,sy);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;translate(-px,-py);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;画布旋转</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;1.以画布为原点，顺时针旋转40.0f度</span><span>&nbsp;&nbsp;</span></span></li><li><span><span class="comment">//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.rotate(40.0f);</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;2.以(100.11f,&nbsp;100.22f)为原点，顺时针旋转50.0f度</span><span>&nbsp;&nbsp;</span></span></li><li><span><span class="comment">//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.rotate(50.0f,&nbsp;100.11f,&nbsp;100.22f);</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;相应的，为了加深理解，我们再剖析一下第二个旋转方法</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;，其实系统为我们做的事情是这样的</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rotate(float&nbsp;degrees,&nbsp;float&nbsp;px,&nbsp;float&nbsp;py){</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;translate(px,py);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rotate(degrees);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;translate(-px,-py);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;画布倾斜</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;skew(float&nbsp;sx,float&nbsp;xy);将画布在x及y轴方向上倾斜相应的角度，sx或sy为倾斜角度的tan值，</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;如canvas.skew(1,0);为在x方向上倾斜45度&nbsp;&gt;&gt;&nbsp;tan(45)&nbsp;=&nbsp;1</span><span>&nbsp;&nbsp;</span></span></li><li><span><span class="comment">//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.skew(1,0);</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li></ol></div><pre title="Android 图形用户界面 之 绘图（一）" pre_index="1" source_url="http://emmet1988.iteye.com/blog/1058517" codeable_type="Blog" codeable_id="1058517" style="display: none;" class="java" name="code">class BasicView2Draw extends View{
		
		Paint paint;
		Bitmap bitmap;
		
		public BasicView2Draw(Context context) {
			super(context);
			paint = new Paint(Paint.ANTI_ALIAS_FLAG);
			bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
		}
		
		private Bitmap createBitmap1(){
			Bitmap bitmap1 = Bitmap.createBitmap(100, 100, Config.ARGB_8888);
			Canvas canvas = new Canvas(bitmap1);
			canvas.drawColor(Color.BLUE);
//	  		canvas.drawARGB(0, 0, 0, 0);// 透明色
			canvas.drawBitmap(bitmap, 0, 0, paint);
			canvas.drawText("Hello Android", 25, 55, paint);
			return bitmap1;
		}
		
		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			
			// 绘制位图
			// 1.绘制位图在（10，10）位置上
			canvas.drawBitmap(createBitmap1(), 10, 10, paint);
			// 2. 	canvas.drawBitmap(Bitmap bitmap,Rect src,Rect dest,Paint paint);
			// 		canvas.drawBitmap(Bitmap bitmap,Rect src,RectF dest,Paint paint);
			// 	绘制位图到一个指定的矩形dest中，位图会自动进行平移和缩放等操作，如果src的参数不为null
			// 则会裁剪位图的部分区域来进行绘制
			Rect rect = new Rect(10, 10, 50, 60);
			RectF rectF1 = new RectF(180.0f, 20.0f, 240.0f, 80.0f);
			RectF rectF2 = new RectF(180.0f, 100.0f, 240.0f, 160.0f);
			canvas.drawBitmap(createBitmap1(), null, rectF1, paint);
			canvas.drawBitmap(createBitmap1(), rect, rectF2, paint);
			
			// 点
			paint.setStyle(Paint.Style.FILL_AND_STROKE);
			paint.setStrokeWidth(5.0f);
			paint.setColor(Color.YELLOW);
			canvas.drawPoints(new float[]{120,120,140,140,160,160,180,180}, paint);
			
			// 线
			paint.reset();// 重置画笔
			paint.setColor(Color.GREEN);
			paint.setAntiAlias(true);
			canvas.drawLine(30, 30, 130, 40, paint);
			paint.setColor(Color.RED);
			canvas.drawLines(new float[]{  40,40,140,40  ,50,50,90,90  }, paint);
			
			// 矩形
			paint.setColor(Color.CYAN);
			canvas.drawRect(10, 150, 150, 250, paint);
			paint.setColor(Color.GRAY);
			canvas.drawRect(new Rect(10, 260, 150, 280), paint);
			paint.setColor(Color.DKGRAY);
			canvas.drawRect(new RectF(20.2f, 290.9f, 120.2f, 300.3f), paint);
			
			// 绘制文本
//			paint.setTextSize(20);
//			paint.setColor(0x40ffffff);// 半透明白色
//			paint.setTextAlign(Paint.Align.RIGHT);// 对齐方向
//			canvas.drawText("Cool Android", 250, 180, paint);// 这里注意，坐标(180,180)是文本的左下点坐标
			
			// 画布平移：
			// 平移的单位是像素，分别是在x,y轴上平移的像素点
			// 正数代表的正方向，x轴为平面的右侧，y轴为平面的下方，相应的，负数则向反方向平移
//			canvas.translate(30.0f, 30.0f);
			
			// 画布缩放：
			// 参数分别是在想x，y轴上放大或缩小的倍数，大雨1为放大，小于1为缩小，
			// 缩放的原点默认为画布的原点(0,0)，也可以指定缩放的原点
//			canvas.scale(2.0f, 1.5f);
//			canvas.scale(0.5f, 0.5f, 100.0f, 100.0f);// 指定坐标(100.0f,100.0f)为缩放原点
			// 这里剖析一下第二个缩放方法，其实系统为我们做的事情是这样的
			/*
			   scale(float sx, float sy, float px, float py){
					translate(px,py);
					scale(sx,sy);
					translate(-px,-py);
				}
			*/
			
			// 画布旋转
			// 1.以画布为原点，顺时针旋转40.0f度
//			canvas.rotate(40.0f);
			// 2.以(100.11f, 100.22f)为原点，顺时针旋转50.0f度
//			canvas.rotate(50.0f, 100.11f, 100.22f);
			// 相应的，为了加深理解，我们再剖析一下第二个旋转方法
			// ，其实系统为我们做的事情是这样的
			/*
			   rotate(float degrees, float px, float py){
					translate(px,py);
					rotate(degrees);
					translate(-px,-py);
				}
			*/
			
			// 画布倾斜
			// skew(float sx,float xy);将画布在x及y轴方向上倾斜相应的角度，sx或sy为倾斜角度的tan值，
			// 如canvas.skew(1,0);为在x方向上倾斜45度 &gt;&gt; tan(45) = 1
//			canvas.skew(1,0);
		}
	}</pre>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>ClipRectDraw.java</p>
<p>&nbsp;</p>
<div id="" class="dp-highlighter"><div class="bar"><div class="tools">Java代码 <embed wmode="transparent" src="1058517_files/clipboard_new.swf" flashvars="clipboard=package%20com.view%3B%0A%0Aimport%20android.content.Context%3B%0Aimport%20android.graphics.Canvas%3B%0Aimport%20android.graphics.Color%3B%0Aimport%20android.graphics.Paint%3B%0Aimport%20android.graphics.Path%3B%0Aimport%20android.graphics.Region%3B%0Aimport%20android.util.AttributeSet%3B%0Aimport%20android.view.View%3B%0A%0A%2F**%0A%20*%20%20---------------------------------------------------%E7%9F%A9%E5%BD%A2%E5%8C%BA%E5%9F%9F-------------------------------------------------%0A%20*%20%20canvas.clipRect(%E5%B7%A6%E4%B8%8A%E8%A7%92x%E8%BD%B4%E5%9D%90%E6%A0%87%2C%20%E5%B7%A6%E4%B8%8A%E8%A7%92y%E8%BD%B4%E5%9D%90%E6%A0%87%2C%20%E5%8F%B3%E4%B8%8B%E8%A7%92x%E8%BD%B4%E5%9D%90%E6%A0%87%2C%20%E5%8F%B3%E4%B8%8B%E8%A7%92y%E8%BD%B4%E5%9D%90%E6%A0%87%2C%20Region.Op.XOR)%3B%0A%20*%20%20%E6%9C%80%E5%90%8E%E4%B8%80%E4%B8%AA%E5%8F%82%E6%95%B0%E6%9C%89%E5%A4%9A%E4%B8%AA%E9%80%89%E6%8B%A9%E5%88%86%E5%88%AB%E6%98%AF%EF%BC%9A%0A%20*%20%20%2F%2FDIFFERENCE%E6%98%AF%E7%AC%AC%E4%B8%80%E6%AC%A1%E4%B8%8D%E5%90%8C%E4%BA%8E%E7%AC%AC%E4%BA%8C%E6%AC%A1%E7%9A%84%E9%83%A8%E5%88%86%E6%98%BE%E7%A4%BA%E5%87%BA%E6%9D%A5%20%0A%20%20%20%20%2F%2FREPLACE%E6%98%AF%E6%98%BE%E7%A4%BA%E7%AC%AC%E4%BA%8C%E6%AC%A1%E7%9A%84%20%0A%20%20%20%20%2F%2FREVERSE_DIFFERENCE%20%E6%98%AF%E7%AC%AC%E4%BA%8C%E6%AC%A1%E4%B8%8D%E5%90%8C%E4%BA%8E%E7%AC%AC%E4%B8%80%E6%AC%A1%E7%9A%84%E9%83%A8%E5%88%86%E6%98%BE%E7%A4%BA%20%0A%20%20%20%20%2F%2FINTERSECT%EF%BC%9A%E4%BA%A4%E9%9B%86%E6%98%BE%E7%A4%BA%20%0A%20%20%20%20%2F%2FUNION%EF%BC%9A%E5%85%A8%E9%83%A8%E6%98%BE%E7%A4%BA%20%0A%20%20%20%20%2F%2FXOR%E8%A1%A5%E9%9B%86%EF%BC%8C%E5%B0%B1%E6%98%AF%E5%85%A8%E9%9B%86%E7%9A%84%E5%87%8F%E5%8E%BB%E4%BA%A4%E9%9B%86%E5%89%A9%E4%BD%99%E9%83%A8%E5%88%86%E6%98%BE%E7%A4%BA%0A%20%20%20%20%0A%20*%20%40author%20emmet1988.iteye.com%0A%20*%0A%20*%2F%0Apublic%20class%20ClipRectDraw%20extends%20View%20%7B%0A%09%0A%09Context%20context%3B%0A%09Paint%20paint%3B%0A%09Path%20path%3B%0A%09%0A%09public%20ClipRectDraw(Context%20context)%20%7B%0A%09%09super(context)%3B%0A%09%09init()%3B%0A%09%7D%0A%09%0A%09public%20ClipRectDraw(Context%20context%2C%20AttributeSet%20attrs)%20%7B%0A%09%09super(context%2C%20attrs)%3B%0A%09%09init()%3B%0A%09%7D%0A%09%0A%09public%20ClipRectDraw(Context%20context%2C%20AttributeSet%20attrs%2C%20int%20defStyle)%20%7B%0A%09%09super(context%2C%20attrs%2C%20defStyle)%3B%0A%09%09init()%3B%0A%09%7D%0A%09%0A%09private%20void%20init()%7B%0A%09%09paint%20%3D%20new%20Paint()%3B%0A%09%09paint.setAntiAlias(true)%3B%0A%09%09paint.setStrokeWidth(5)%3B%0A%09%09paint.setTextSize(15)%3B%0A%09%09paint.setTextAlign(Paint.Align.RIGHT)%3B%0A%09%09path%20%3D%20new%20Path()%3B%0A%09%7D%0A%09%0A%09%40Override%0A%09protected%20void%20onDraw(Canvas%20canvas)%20%7B%0A%09%09super.onDraw(canvas)%3B%0A%09%09canvas.drawColor(Color.GRAY)%3B%0A%09%09%2F%2F%E5%B7%A6%E4%B8%8A%E5%9B%BE%0A%09%09canvas.save()%3B%0A%09%09canvas.translate(10%2C%2010)%3B%0A%09%09drawScene(canvas)%3B%0A%09%09canvas.restore()%3B%0A%09%09%2F%2F%E5%8F%B3%E4%B8%8A%E5%9B%BE%0A%09%09canvas.save()%3B%0A%09%09canvas.translate(160%2C%2010)%3B%0A%09%09canvas.clipRect(10%2C%2010%2C%2090%2C%2090)%3B%0A%09%09canvas.clipRect(30%2C%2030%2C%2070%2C%2070%2C%20Region.Op.XOR)%3B%0A%09%09drawScene(canvas)%3B%0A%09%09canvas.restore()%3B%0A%09%09%2F%2F%E5%B7%A6%E4%B8%AD%E5%9B%BE%0A%09%09canvas.save()%3B%0A%09%09canvas.translate(10%2C%20130)%3B%0A%09%09path.reset()%3B%0A%09%09%2F*%E6%8A%9B%E7%89%A9%E6%9B%B2%E7%BA%BF*%2F%0A%09%09path.cubicTo(0%2C%200%2C%20100%2C%200%2C%20100%2C%20100)%3B%0A%09%09path.cubicTo(100%2C%20100%2C%200%2C%20100%2C%200%2C%200)%3B%0A%09%09canvas.clipPath(path%2C%20Region.Op.REPLACE)%3B%0A%09%09drawScene(canvas)%3B%0A%09%09canvas.restore()%3B%0A%09%09%2F%2F%E5%8F%B3%E4%B8%AD%E5%9B%BE%0A%09%09canvas.save()%3B%0A%20%20%20%20%20%20%20%20canvas.translate(160%2C%20130)%3B%0A%20%20%20%20%20%20%20%20canvas.clipRect(0%2C%200%2C%2060%2C%2060)%3B%0A%20%20%20%20%20%20%20%20canvas.clipRect(40%2C%2040%2C%20100%2C%20100%2C%20Region.Op.UNION)%3B%0A%20%20%20%20%20%20%20%20drawScene(canvas)%3B%0A%20%20%20%20%20%20%20%20canvas.restore()%3B%0A%20%20%20%20%20%20%20%20%2F%2F%E5%B7%A6%E4%B8%8B%E5%9B%BE%0A%20%20%20%20%20%20%20%20canvas.save()%3B%0A%20%20%20%20%20%20%20%20canvas.translate(10%2C%20250)%3B%0A%20%20%20%20%20%20%20%20canvas.clipRect(0%2C%200%2C%2060%2C%2060)%3B%0A%20%20%20%20%20%20%20%20canvas.clipRect(40%2C%2040%2C%20100%2C%20100%2C%20Region.Op.XOR)%3B%0A%20%20%20%20%20%20%20%20drawScene(canvas)%3B%0A%20%20%20%20%20%20%20%20canvas.restore()%3B%0A%20%20%20%20%20%20%20%20%2F%2F%E5%8F%B3%E4%B8%8B%E5%9B%BE%0A%20%20%20%20%20%20%20%20canvas.translate(160%2C%20250)%3B%0A%20%20%20%20%20%20%20%20canvas.clipRect(0%2C%200%2C%2060%2C%2060)%3B%0A%20%20%20%20%20%20%20%20canvas.clipRect(40%2C%2040%2C%20100%2C%20100%2C%20Region.Op.REVERSE_DIFFERENCE)%3B%0A%20%20%20%20%20%20%20%20drawScene(canvas)%3B%0A%20%20%20%20%20%20%20%20canvas.restore()%3B%0A%09%7D%0A%09%0A%09private%20void%20drawScene(Canvas%20canvas)%7B%0A%09%09canvas.clipRect(0%2C%200%2C%20100%2C%20100)%3B%0A%09%09canvas.drawColor(Color.WHITE)%3B%0A%09%09%0A%09%09paint.setColor(Color.RED)%3B%0A%09%09canvas.drawLine(0%2C%200%2C%20100%2C%20100%2C%20paint)%3B%0A%09%09%0A%09%09paint.setColor(Color.GREEN)%3B%0A%09%09canvas.drawCircle(30%2C%2070%2C%2030%2C%20paint)%3B%0A%09%09%0A%09%09paint.setColor(Color.BLUE)%3B%0A%09%09canvas.drawText(%22ChenJianLi%22%2C%20100%2C%2030%2C%20paint)%3B%0A%09%7D%0A%0A%7D%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="15" width="14">&nbsp;<a href="javascript:void()" title="收藏这段代码" onclick="code_favorites_do_favorite(this);return false;"><img class="star" src="1058517_files/icon_star.png" alt="收藏代码"><img class="spinner" src="1058517_files/spinner.gif" style="display:none"></a></div></div><ol class="dp-j" start="1"><li><span><span class="keyword">package</span><span>&nbsp;com.view;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span><span class="keyword">import</span><span>&nbsp;android.content.Context;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.Canvas;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.Color;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.Paint;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.Path;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.Region;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.util.AttributeSet;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.view.View;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span><span class="comment">/**</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;*&nbsp;&nbsp;---------------------------------------------------矩形区域-------------------------------------------------</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;*&nbsp;&nbsp;canvas.clipRect(左上角x轴坐标,&nbsp;左上角y轴坐标,&nbsp;右下角x轴坐标,&nbsp;右下角y轴坐标,&nbsp;Region.Op.XOR);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;*&nbsp;&nbsp;最后一个参数有多个选择分别是：</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;*&nbsp;&nbsp;//DIFFERENCE是第一次不同于第二次的部分显示出来&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;//REPLACE是显示第二次的&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;//REVERSE_DIFFERENCE&nbsp;是第二次不同于第一次的部分显示&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;//INTERSECT：交集显示&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;//UNION：全部显示&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;//XOR补集，就是全集的减去交集剩余部分显示</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;*&nbsp;@author&nbsp;emmet1988.iteye.com</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;*</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">public</span><span>&nbsp;</span><span class="keyword">class</span><span>&nbsp;ClipRectDraw&nbsp;</span><span class="keyword">extends</span><span>&nbsp;View&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;Context&nbsp;context;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;Paint&nbsp;paint;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;Path&nbsp;path;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;ClipRectDraw(Context&nbsp;context)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">super</span><span>(context);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;init();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;ClipRectDraw(Context&nbsp;context,&nbsp;AttributeSet&nbsp;attrs)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">super</span><span>(context,&nbsp;attrs);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;init();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;ClipRectDraw(Context&nbsp;context,&nbsp;AttributeSet&nbsp;attrs,&nbsp;</span><span class="keyword">int</span><span>&nbsp;defStyle)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">super</span><span>(context,&nbsp;attrs,&nbsp;defStyle);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;init();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">private</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;init(){&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Paint();&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setAntiAlias(<span class="keyword">true</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setStrokeWidth(<span class="number">5</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setTextSize(<span class="number">15</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setTextAlign(Paint.Align.RIGHT);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Path();&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="annotation">@Override</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">protected</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;onDraw(Canvas&nbsp;canvas)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">super</span><span>.onDraw(canvas);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawColor(Color.GRAY);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//左上图</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.save();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.translate(<span class="number">10</span><span>,&nbsp;</span><span class="number">10</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;drawScene(canvas);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.restore();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//右上图</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.save();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.translate(<span class="number">160</span><span>,&nbsp;</span><span class="number">10</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.clipRect(<span class="number">10</span><span>,&nbsp;</span><span class="number">10</span><span>,&nbsp;</span><span class="number">90</span><span>,&nbsp;</span><span class="number">90</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.clipRect(<span class="number">30</span><span>,&nbsp;</span><span class="number">30</span><span>,&nbsp;</span><span class="number">70</span><span>,&nbsp;</span><span class="number">70</span><span>,&nbsp;Region.Op.XOR);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;drawScene(canvas);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.restore();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//左中图</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.save();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.translate(<span class="number">10</span><span>,&nbsp;</span><span class="number">130</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path.reset();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*抛物曲线*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path.cubicTo(<span class="number">0</span><span>,&nbsp;</span><span class="number">0</span><span>,&nbsp;</span><span class="number">100</span><span>,&nbsp;</span><span class="number">0</span><span>,&nbsp;</span><span class="number">100</span><span>,&nbsp;</span><span class="number">100</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path.cubicTo(<span class="number">100</span><span>,&nbsp;</span><span class="number">100</span><span>,&nbsp;</span><span class="number">0</span><span>,&nbsp;</span><span class="number">100</span><span>,&nbsp;</span><span class="number">0</span><span>,&nbsp;</span><span class="number">0</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.clipPath(path,&nbsp;Region.Op.REPLACE);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;drawScene(canvas);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.restore();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//右中图</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.save();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.translate(<span class="number">160</span><span>,&nbsp;</span><span class="number">130</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.clipRect(<span class="number">0</span><span>,&nbsp;</span><span class="number">0</span><span>,&nbsp;</span><span class="number">60</span><span>,&nbsp;</span><span class="number">60</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.clipRect(<span class="number">40</span><span>,&nbsp;</span><span class="number">40</span><span>,&nbsp;</span><span class="number">100</span><span>,&nbsp;</span><span class="number">100</span><span>,&nbsp;Region.Op.UNION);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;drawScene(canvas);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.restore();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//左下图</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.save();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.translate(<span class="number">10</span><span>,&nbsp;</span><span class="number">250</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.clipRect(<span class="number">0</span><span>,&nbsp;</span><span class="number">0</span><span>,&nbsp;</span><span class="number">60</span><span>,&nbsp;</span><span class="number">60</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.clipRect(<span class="number">40</span><span>,&nbsp;</span><span class="number">40</span><span>,&nbsp;</span><span class="number">100</span><span>,&nbsp;</span><span class="number">100</span><span>,&nbsp;Region.Op.XOR);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;drawScene(canvas);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.restore();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//右下图</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.translate(<span class="number">160</span><span>,&nbsp;</span><span class="number">250</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.clipRect(<span class="number">0</span><span>,&nbsp;</span><span class="number">0</span><span>,&nbsp;</span><span class="number">60</span><span>,&nbsp;</span><span class="number">60</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.clipRect(<span class="number">40</span><span>,&nbsp;</span><span class="number">40</span><span>,&nbsp;</span><span class="number">100</span><span>,&nbsp;</span><span class="number">100</span><span>,&nbsp;Region.Op.REVERSE_DIFFERENCE);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;drawScene(canvas);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.restore();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">private</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;drawScene(Canvas&nbsp;canvas){&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.clipRect(<span class="number">0</span><span>,&nbsp;</span><span class="number">0</span><span>,&nbsp;</span><span class="number">100</span><span>,&nbsp;</span><span class="number">100</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawColor(Color.WHITE);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setColor(Color.RED);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawLine(<span class="number">0</span><span>,&nbsp;</span><span class="number">0</span><span>,&nbsp;</span><span class="number">100</span><span>,&nbsp;</span><span class="number">100</span><span>,&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setColor(Color.GREEN);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawCircle(<span class="number">30</span><span>,&nbsp;</span><span class="number">70</span><span>,&nbsp;</span><span class="number">30</span><span>,&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setColor(Color.BLUE);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawText(<span class="string">"ChenJianLi"</span><span>,&nbsp;</span><span class="number">100</span><span>,&nbsp;</span><span class="number">30</span><span>,&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li><span>}&nbsp;&nbsp;</span></li></ol></div><pre title="Android 图形用户界面 之 绘图（一）" pre_index="2" source_url="http://emmet1988.iteye.com/blog/1058517" codeable_type="Blog" codeable_id="1058517" style="display: none;" class="java" name="code">package com.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Region;
import android.util.AttributeSet;
import android.view.View;

/**
 *  ---------------------------------------------------矩形区域-------------------------------------------------
 *  canvas.clipRect(左上角x轴坐标, 左上角y轴坐标, 右下角x轴坐标, 右下角y轴坐标, Region.Op.XOR);
 *  最后一个参数有多个选择分别是：
 *  //DIFFERENCE是第一次不同于第二次的部分显示出来 
    //REPLACE是显示第二次的 
    //REVERSE_DIFFERENCE 是第二次不同于第一次的部分显示 
    //INTERSECT：交集显示 
    //UNION：全部显示 
    //XOR补集，就是全集的减去交集剩余部分显示
    
 * @author emmet1988.iteye.com
 *
 */
public class ClipRectDraw extends View {
	
	Context context;
	Paint paint;
	Path path;
	
	public ClipRectDraw(Context context) {
		super(context);
		init();
	}
	
	public ClipRectDraw(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	
	public ClipRectDraw(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}
	
	private void init(){
		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setStrokeWidth(5);
		paint.setTextSize(15);
		paint.setTextAlign(Paint.Align.RIGHT);
		path = new Path();
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawColor(Color.GRAY);
		//左上图
		canvas.save();
		canvas.translate(10, 10);
		drawScene(canvas);
		canvas.restore();
		//右上图
		canvas.save();
		canvas.translate(160, 10);
		canvas.clipRect(10, 10, 90, 90);
		canvas.clipRect(30, 30, 70, 70, Region.Op.XOR);
		drawScene(canvas);
		canvas.restore();
		//左中图
		canvas.save();
		canvas.translate(10, 130);
		path.reset();
		/*抛物曲线*/
		path.cubicTo(0, 0, 100, 0, 100, 100);
		path.cubicTo(100, 100, 0, 100, 0, 0);
		canvas.clipPath(path, Region.Op.REPLACE);
		drawScene(canvas);
		canvas.restore();
		//右中图
		canvas.save();
        canvas.translate(160, 130);
        canvas.clipRect(0, 0, 60, 60);
        canvas.clipRect(40, 40, 100, 100, Region.Op.UNION);
        drawScene(canvas);
        canvas.restore();
        //左下图
        canvas.save();
        canvas.translate(10, 250);
        canvas.clipRect(0, 0, 60, 60);
        canvas.clipRect(40, 40, 100, 100, Region.Op.XOR);
        drawScene(canvas);
        canvas.restore();
        //右下图
        canvas.translate(160, 250);
        canvas.clipRect(0, 0, 60, 60);
        canvas.clipRect(40, 40, 100, 100, Region.Op.REVERSE_DIFFERENCE);
        drawScene(canvas);
        canvas.restore();
	}
	
	private void drawScene(Canvas canvas){
		canvas.clipRect(0, 0, 100, 100);
		canvas.drawColor(Color.WHITE);
		
		paint.setColor(Color.RED);
		canvas.drawLine(0, 0, 100, 100, paint);
		
		paint.setColor(Color.GREEN);
		canvas.drawCircle(30, 70, 30, paint);
		
		paint.setColor(Color.BLUE);
		canvas.drawText("ChenJianLi", 100, 30, paint);
	}

}
</pre>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>MatrixDraw.java</p>
<p>&nbsp;</p>
<div id="" class="dp-highlighter"><div class="bar"><div class="tools">Java代码 <embed wmode="transparent" src="1058517_files/clipboard_new.swf" flashvars="clipboard=package%20com.view%3B%0A%0Aimport%20com.test.R%3B%0A%0Aimport%20android.content.Context%3B%0Aimport%20android.graphics.Bitmap%3B%0Aimport%20android.graphics.Canvas%3B%0Aimport%20android.graphics.Matrix%3B%0Aimport%20android.graphics.Paint%3B%0Aimport%20android.graphics.drawable.BitmapDrawable%3B%0Aimport%20android.view.View%3B%0A%09%2F**%0A%09%20*%20%E5%9C%A8%20Android%20%E9%87%8C%E9%9D%A2%EF%BC%8C%20Matrix%20%E7%94%B1%209%20%E4%B8%AA%20float%20%E5%80%BC%E6%9E%84%E6%88%90%EF%BC%8C%E6%98%AF%E4%B8%80%E4%B8%AA%203*3%20%E7%9A%84%E7%9F%A9%E9%98%B5%E3%80%82%0A%09%20*%20cosX%2C%20-sinX%2CtranslateX%09%0A%09%20*%20sinX%2C%20%20cosX%2CtranslateY%0A%09%20*%200%2C%09%09%09%090%2C%09%09%09scale%0A%09%20*%20%E8%A7%A3%E9%87%8A%E4%B8%80%E4%B8%8B%EF%BC%8C%E4%B8%8A%E9%9D%A2%E7%9A%84%20sinX%20%E5%92%8C%20cosX%20%EF%BC%8C%E8%A1%A8%E7%A4%BA%E6%97%8B%E8%BD%AC%E8%A7%92%E5%BA%A6%E7%9A%84%20cos%20%E5%80%BC%E5%92%8C%20sin%20%E5%80%BC%EF%BC%8C%E6%B3%A8%E6%84%8F%EF%BC%8C%0A%09%20*%20%E6%97%8B%E8%BD%AC%E8%A7%92%E5%BA%A6%E6%98%AF%E6%8C%89%E9%A1%BA%E6%97%B6%E9%92%88%E6%96%B9%E5%90%91%E8%AE%A1%E7%AE%97%E7%9A%84%E3%80%82%20translateX%20%E5%92%8C%20translateY%20%E8%A1%A8%E7%A4%BA%20x%20%E5%92%8C%20y%20%E7%9A%84%E5%B9%B3%E7%A7%BB%E9%87%8F%E3%80%82%0A%09%20*%20scale%20%E6%98%AF%E7%BC%A9%E6%94%BE%E7%9A%84%E6%AF%94%E4%BE%8B%EF%BC%8C%201%20%E6%98%AF%E4%B8%8D%E5%8F%98%EF%BC%8C%202%20%E6%98%AF%E8%A1%A8%E7%A4%BA%E7%BC%A9%E6%94%BE%201%2F2%20%EF%BC%8C%0A%09%20*%20%40author%20emmet1988.iteye.com%0A%09%20*%0A%09%20*%2F%0Apublic%20class%20MatrixDraw%20extends%20View%20implements%20Runnable%7B%0A%0A%09Bitmap%20bitmap%3B%0A%09Matrix%20matrix%20%3D%20new%20Matrix()%3B%0A%09Paint%20paint%3B%0A%09%0A%09public%20MatrixDraw(Context%20context)%20%7B%0A%09%09super(context)%3B%0A%09%09bitmap%20%3D%20((BitmapDrawable)getResources().getDrawable(R.drawable.rotate_surfaceview)).getBitmap()%3B%0A%09%09paint%20%3D%20new%20Paint()%3B%0A%09%09paint.setAntiAlias(true)%3B%0A%09%09new%20Thread(this).start()%3B%0A%09%7D%0A%09%0A%09float%20m%3B%0A%09float%20n%3B%0A%09%40Override%0A%09protected%20void%20onDraw(Canvas%20canvas)%20%7B%0A%09%09%0A%09%09%2F*%0A%09%09float%20cosValue%20%3D%20(float)Math.cos(-Math.PI%2Fm)%3B%0A%09%09float%20sinValue%20%3D%20(float)Math.sin(-Math.PI%2Fm)%3B%0A%09%09%0A%09%09Log.d(%22matrixdraw%22%2C%20%22Math.PI%20%3D%22%20%2B%20Math.PI)%3B%0A%09%09Log.d(%22matrixdraw%22%2C%20%22Math.PI%2Fm%20%3D%22%20%2B%20Math.PI%2Fm)%3B%0A%09%09Log.d(%22matrixdraw%22%2C%20%22Math.cos(-Math.PI%2Fm)%20%3D%22%20%2B%20(float)Math.cos(-Math.PI%2Fm))%3B%0A%09%09Log.d(%22matrixdraw%22%2C%20%22Math.sin(-Math.PI%2Fm)%20%3D%22%20%2B%20(float)Math.sin(-Math.PI%2Fm))%3B%0A%09%09%0A%09%09matrix.setValues(new%20float%5B%5D%7B%0A%09%09%09%09cosValue%2C-sinValue%2C100%2C%0A%09%09%09%09sinValue%2CcosValue%2C100%2C%0A%09%09%09%090%2C%200%2C%202%0A%09%09%7D)%3B%2F%2F%E4%B8%BE%E4%BE%8B%EF%BC%8C%E8%8B%A5%E7%BC%A9%E6%94%BE%E5%80%BC%E4%B8%BA0.9%EF%BC%8C%E4%BB%A3%E8%A1%A8%E6%94%BE%E5%A4%A7%E5%8E%9F%E5%9B%BE%E7%9A%84%E5%8D%81%E5%88%86%E4%B9%8B%E4%B8%80%0A%2F%2F%09%09%09super.onDraw(canvas)%3B%2F%2F%E5%BD%93%E7%84%B6%EF%BC%8C%E5%A6%82%E6%9E%9C%E7%95%8C%E9%9D%A2%E4%B8%8A%E8%BF%98%E6%9C%89%E5%85%B6%E4%BB%96%E5%85%83%E7%B4%A0%E9%9C%80%E8%A6%81%E7%BB%98%E5%88%B6%EF%BC%8C%E5%8F%AA%E9%9C%80%E8%A6%81%E5%B0%86%E8%BF%99%E5%8F%A5%E8%AF%9D%E5%86%99%E4%B8%8A%E5%B0%B1%E8%A1%8C%E4%BA%86%E3%80%82%20%0A%09%09%0A%2F%2F%09%09Matrix%20matrix2%20%3D%20new%20Matrix(matrix)%3B%0A%09%09canvas.drawBitmap(bitmap%2C%20matrix%2C%20paint)%3B%0A%2F%2F%09%09canvas.drawBitmap(bitmap%2C%20matrix2%2C%20paint)%3B%0A*%2F%09%09%0A%09%09n%20%2B%2B%3B%0A%09%09if%20(n%20%3D%3D%2060)%20%7B%0A%09%09%09n%20%3D%200%3B%0A%09%09%7D%0A%09%09matrix.postRotate(n)%3B%0A%09%09matrix.postTranslate(n%2C%20n)%3B%0A%09%09matrix.postScale(1%2C%201%2C%20n%2C%20n)%3B%0A%09%09canvas.drawBitmap(bitmap%2C%20matrix%2C%20paint)%3B%0A%09%7D%0A%0A%09%40Override%0A%09public%20void%20run()%20%7B%0A%09%09while(!Thread.currentThread().isInterrupted())%7B%0A%09%09%09try%20%7B%0A%09%09%09%09Thread.sleep(100)%3B%0A%09%09%09%09postInvalidate()%3B%0A%09%09%09%7D%20catch%20(InterruptedException%20e)%20%7B%0A%09%09%09%09Thread.currentThread().interrupt()%3B%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%0A%09%2F**%0A%09%20*%20%20%E4%BB%A5%E5%B7%A6%E4%B8%8A%E8%A7%92%E4%B8%BA%E9%A1%B6%E7%82%B9%EF%BC%8C%E7%BC%A9%E6%94%BE%E4%B8%80%E5%8D%8A%EF%BC%8C%E9%80%86%E6%97%B6%E9%92%88%E6%97%8B%E8%BD%AC30%E5%BA%A6%EF%BC%8C%0A%09%20*%20%20%E7%84%B6%E5%90%8E%E6%B2%BFx%E8%BD%B4%E5%92%8Cy%E8%BD%B4%E5%88%86%E5%88%AB%E5%B9%B3%E7%A7%BB50%E4%B8%AA%E5%83%8F%E7%B4%A0%EF%BC%8C%0A%09%20*%20%20%E4%BB%A3%E7%A0%81%20%E9%87%8C%E9%9D%A2%E5%86%99%E7%9A%84%E6%98%AF100%EF%BC%8C%E4%B8%BA%E4%BB%80%E4%B9%88%E6%98%AF%E5%B9%B3%E7%A7%BB50%E5%91%A2%EF%BC%8C%0A%09%20*%20%20%E5%9B%A0%E4%B8%BA%E7%BC%A9%E6%94%BE%E4%BA%86%E4%B8%80%E5%8D%8A%E3%80%82%20%0A%09%20*%20%20%E5%A4%A7%E5%AE%B6%E5%8F%AF%E4%BB%A5%E8%87%AA%E5%B7%B1%E8%AE%BE%E7%BD%AE%E4%B8%80%E4%B8%8BMatrix%E7%9A%84%E5%80%BC%EF%BC%8C%E6%88%96%E8%80%85%E5%B0%9D%E8%AF%95%E4%B8%80%E4%B8%8B%E4%B8%A4%E4%B8%AA%0A%09%20*%20%20Matrix%E7%9B%B8%E4%B9%98%EF%BC%8C%E5%BE%97%E5%88%B0%E7%9A%84%E5%80%BC%E8%AE%BE%E7%BD%AE%E8%BF%9B%E5%8E%BB%EF%BC%8C%0A%09%20*%20%20%E8%BF%99%E6%A0%B7%E6%89%8D%E8%83%BD%E5%AF%B9Matrix%E6%9B%B4%E5%8A%A0%E7%86%9F%E7%BB%83%E3%80%82%0A%09%20*%2F%0A%09%0A%7D%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="15" width="14">&nbsp;<a href="javascript:void()" title="收藏这段代码" onclick="code_favorites_do_favorite(this);return false;"><img class="star" src="1058517_files/icon_star.png" alt="收藏代码"><img class="spinner" src="1058517_files/spinner.gif" style="display:none"></a></div></div><ol class="dp-j" start="1"><li><span><span class="keyword">package</span><span>&nbsp;com.view;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span><span class="keyword">import</span><span>&nbsp;com.test.R;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span><span class="keyword">import</span><span>&nbsp;android.content.Context;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.Bitmap;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.Canvas;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.Matrix;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.Paint;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.drawable.BitmapDrawable;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.view.View;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/**</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;
在&nbsp;Android&nbsp;里面，&nbsp;Matrix&nbsp;由&nbsp;9&nbsp;
个&nbsp;float&nbsp;值构成，是一个&nbsp;3*3&nbsp;的矩阵。</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;cosX,&nbsp;-sinX,translateX&nbsp;&nbsp;&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;sinX,&nbsp;&nbsp;cosX,translateY</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;0,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scale</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;解释一下，上面的&nbsp;sinX&nbsp;和&nbsp;cosX&nbsp;，表示旋转角度的&nbsp;cos&nbsp;值和&nbsp;sin&nbsp;值，注意，</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;
旋转角度是按顺时针方向计算的。&nbsp;translateX&nbsp;和&nbsp;translateY&nbsp;表
示&nbsp;x&nbsp;和&nbsp;y&nbsp;的平移量。</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;scale&nbsp;是缩放的比例，&nbsp;1&nbsp;是不变，&nbsp;2&nbsp;是表示缩放&nbsp;1/2&nbsp;，</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@author&nbsp;emmet1988.iteye.com</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">public</span><span>&nbsp;</span><span class="keyword">class</span><span>&nbsp;MatrixDraw&nbsp;</span><span class="keyword">extends</span><span>&nbsp;View&nbsp;</span><span class="keyword">implements</span><span>&nbsp;Runnable{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;Bitmap&nbsp;bitmap;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;Matrix&nbsp;matrix&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Matrix();&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;Paint&nbsp;paint;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;MatrixDraw(Context&nbsp;context)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">super</span><span>(context);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bitmap&nbsp;=&nbsp;((BitmapDrawable)getResources().getDrawable(R.drawable.rotate_surfaceview)).getBitmap();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Paint();&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setAntiAlias(<span class="keyword">true</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">new</span><span>&nbsp;Thread(</span><span class="keyword">this</span><span>).start();&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">float</span><span>&nbsp;m;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">float</span><span>&nbsp;n;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="annotation">@Override</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">protected</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;onDraw(Canvas&nbsp;canvas)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;float&nbsp;cosValue&nbsp;=&nbsp;(float)Math.cos(-Math.PI/m);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;float&nbsp;sinValue&nbsp;=&nbsp;(float)Math.sin(-Math.PI/m);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.d("matrixdraw",&nbsp;"Math.PI&nbsp;="&nbsp;+&nbsp;Math.PI);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.d("matrixdraw",&nbsp;"Math.PI/m&nbsp;="&nbsp;+&nbsp;Math.PI/m);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.d("matrixdraw",&nbsp;"Math.cos(-Math.PI/m)&nbsp;="&nbsp;+&nbsp;(float)Math.cos(-Math.PI/m));</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.d("matrixdraw",&nbsp;"Math.sin(-Math.PI/m)&nbsp;="&nbsp;+&nbsp;(float)Math.sin(-Math.PI/m));</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matrix.setValues(new&nbsp;float[]{</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cosValue,-sinValue,100,</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sinValue,cosValue,100,</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0,&nbsp;0,&nbsp;2</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});//举例，若缩放值为0.9，代表放大原图的十分之一</span>&nbsp;</span></li><li><span><span class="comment">//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super.onDraw(canvas);//当然，如果界面上还有其他元素需要绘制，只需要将这句话写上就行了。&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Matrix&nbsp;matrix2&nbsp;=&nbsp;new&nbsp;Matrix(matrix);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawBitmap(bitmap,&nbsp;matrix,&nbsp;paint);</span>&nbsp;</span></li><li><span><span class="comment">//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawBitmap(bitmap,&nbsp;matrix2,&nbsp;paint);</span>&nbsp;</span></li><li><span><span class="comment">*/</span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n&nbsp;++;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">if</span><span>&nbsp;(n&nbsp;==&nbsp;</span><span class="number">60</span><span>)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n&nbsp;=&nbsp;<span class="number">0</span><span>;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matrix.postRotate(n);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matrix.postTranslate(n,&nbsp;n);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matrix.postScale(<span class="number">1</span><span>,&nbsp;</span><span class="number">1</span><span>,&nbsp;n,&nbsp;n);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawBitmap(bitmap,&nbsp;matrix,&nbsp;paint);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="annotation">@Override</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;run()&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">while</span><span>(!Thread.currentThread().isInterrupted()){&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">try</span><span>&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread.sleep(<span class="number">100</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;postInvalidate();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<span class="keyword">catch</span><span>&nbsp;(InterruptedException&nbsp;e)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread.currentThread().interrupt();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/**</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;以左上角为顶点，缩放一半，逆时针旋转30度，</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;然后沿x轴和y轴分别平移50个像素，</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;代码&nbsp;里面写的是100，为什么是平移50呢，</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;因为缩放了一半。&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;大家可以自己设置一下Matrix的值，或者尝试一下两个</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;Matrix相乘，得到的值设置进去，</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;这样才能对Matrix更加熟练。</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>}&nbsp;&nbsp;</span></li></ol></div><pre title="Android 图形用户界面 之 绘图（一）" pre_index="3" source_url="http://emmet1988.iteye.com/blog/1058517" codeable_type="Blog" codeable_id="1058517" style="display: none;" class="java" name="code">package com.view;

import com.test.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
	/**
	 * 在 Android 里面， Matrix 由 9 个 float 值构成，是一个 3*3 的矩阵。
	 * cosX, -sinX,translateX	
	 * sinX,  cosX,translateY
	 * 0,				0,			scale
	 * 解释一下，上面的 sinX 和 cosX ，表示旋转角度的 cos 值和 sin 值，注意，
	 * 旋转角度是按顺时针方向计算的。 translateX 和 translateY 表示 x 和 y 的平移量。
	 * scale 是缩放的比例， 1 是不变， 2 是表示缩放 1/2 ，
	 * @author emmet1988.iteye.com
	 *
	 */
public class MatrixDraw extends View implements Runnable{

	Bitmap bitmap;
	Matrix matrix = new Matrix();
	Paint paint;
	
	public MatrixDraw(Context context) {
		super(context);
		bitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.rotate_surfaceview)).getBitmap();
		paint = new Paint();
		paint.setAntiAlias(true);
		new Thread(this).start();
	}
	
	float m;
	float n;
	@Override
	protected void onDraw(Canvas canvas) {
		
		/*
		float cosValue = (float)Math.cos(-Math.PI/m);
		float sinValue = (float)Math.sin(-Math.PI/m);
		
		Log.d("matrixdraw", "Math.PI =" + Math.PI);
		Log.d("matrixdraw", "Math.PI/m =" + Math.PI/m);
		Log.d("matrixdraw", "Math.cos(-Math.PI/m) =" + (float)Math.cos(-Math.PI/m));
		Log.d("matrixdraw", "Math.sin(-Math.PI/m) =" + (float)Math.sin(-Math.PI/m));
		
		matrix.setValues(new float[]{
				cosValue,-sinValue,100,
				sinValue,cosValue,100,
				0, 0, 2
		});//举例，若缩放值为0.9，代表放大原图的十分之一
//			super.onDraw(canvas);//当然，如果界面上还有其他元素需要绘制，只需要将这句话写上就行了。 
		
//		Matrix matrix2 = new Matrix(matrix);
		canvas.drawBitmap(bitmap, matrix, paint);
//		canvas.drawBitmap(bitmap, matrix2, paint);
*/		
		n ++;
		if (n == 60) {
			n = 0;
		}
		matrix.postRotate(n);
		matrix.postTranslate(n, n);
		matrix.postScale(1, 1, n, n);
		canvas.drawBitmap(bitmap, matrix, paint);
	}

	@Override
	public void run() {
		while(!Thread.currentThread().isInterrupted()){
			try {
				Thread.sleep(100);
				postInvalidate();
			} catch (InterruptedException e) {
				Thread.currentThread().interrupt();
			}
		}
	}

	/**
	 *  以左上角为顶点，缩放一半，逆时针旋转30度，
	 *  然后沿x轴和y轴分别平移50个像素，
	 *  代码 里面写的是100，为什么是平移50呢，
	 *  因为缩放了一半。 
	 *  大家可以自己设置一下Matrix的值，或者尝试一下两个
	 *  Matrix相乘，得到的值设置进去，
	 *  这样才能对Matrix更加熟练。
	 */
	
}
</pre>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>Matrix2Draw.java</p>
<p>&nbsp;</p>
<div id="" class="dp-highlighter"><div class="bar"><div class="tools">Java代码 <embed wmode="transparent" src="1058517_files/clipboard_new.swf" flashvars="clipboard=public%20class%20Matrix2Draw%20extends%20View%20%7B%0A%09%09%0A%09%09private%20Bitmap%20mBitmap%3B%0A%09%09private%20Matrix%20mMatrix%20%3D%20new%20Matrix()%3B%0A%09%09%0A%09%09public%20Matrix2Draw(Context%20context)%20%7B%0A%09%09%09super(context)%3B%0A%09%09%09initialize()%3B%0A%09%09%7D%0A%0A%09%09private%20void%20initialize()%20%7B%0A%09%09%09%0A%09%09%09Bitmap%20bmp%20%3D%20((BitmapDrawable)getResources().getDrawable(R.drawable.rotate_surfaceview)).getBitmap()%3B%0A%09%09%09mBitmap%20%3D%20bmp%3B%0A%09%09%09%2F*%E9%A6%96%E5%85%88%EF%BC%8C%E5%B0%86%E7%BC%A9%E6%94%BE%E4%B8%BA100*100%E3%80%82%E8%BF%99%E9%87%8Cscale%E7%9A%84%E5%8F%82%E6%95%B0%E6%98%AF%E6%AF%94%E4%BE%8B%E3%80%82%E6%9C%89%E4%B8%80%E7%82%B9%E8%A6%81%E6%B3%A8%E6%84%8F%EF%BC%8C%E5%A6%82%E6%9E%9C%E7%9B%B4%E6%8E%A5%E7%94%A8100%2F%0A%09%09%09%20%20%20bmp.getWidth()%E7%9A%84%E8%AF%9D%EF%BC%8C%E4%BC%9A%E5%BE%97%E5%88%B00%EF%BC%8C%E5%9B%A0%E4%B8%BA%E6%98%AF%E6%95%B4%E5%9E%8B%E7%9B%B8%E9%99%A4%EF%BC%8C%E6%89%80%E4%BB%A5%E5%BF%85%E9%A1%BB%E5%85%B6%E4%B8%AD%E6%9C%89%E4%B8%80%E4%B8%AA%E6%98%AFfloat%E5%9E%8B%E7%9A%84%EF%BC%8C%0A%09%09%09%20%20%20%E7%9B%B4%E6%8E%A5%E7%94%A8100f%E5%B0%B1%E5%A5%BD%E3%80%82*%2F%0A%09%09%09mMatrix.setScale(100f%2Fbmp.getWidth()%2C%20100f%2Fbmp.getHeight())%3B%0A%09%09%09%2F*%2F%2F%20post%20%E6%96%B9%E5%BC%8F%EF%BC%9A%E5%90%8E%E4%B9%98%20%3E%3E%20%E5%BD%93%E5%89%8D%E7%9A%84%E7%9F%A9%E9%98%B5%E4%B9%98%E4%BB%A5%E5%8F%82%E6%95%B0%E7%BB%99%E5%87%BA%E7%9A%84%E7%9F%A9%E9%98%B5%E3%80%82%E5%8F%AF%E4%BB%A5%E8%BF%9E%E7%BB%AD%E5%A4%9A%E6%AC%A1%E4%BD%BF%E7%94%A8post%EF%BC%8C%E6%9D%A5%E5%AE%8C%E6%88%90%E6%89%80%E9%9C%80%E7%9A%84%E6%95%B4%E4%B8%AA%E5%8F%98%E6%8D%A2%E3%80%82%0A%09%20%20%20%20%20%20%20%20%2F%2F%E5%B9%B3%E7%A7%BB%E5%88%B0%EF%BC%88100%EF%BC%8C100%EF%BC%89%E5%A4%84%0A%09%09%09mMatrix.postTranslate(100%2C%20100)%3B%0A%09%20%20%20%20%20%20%20%20%2F%2F%E5%80%BE%E6%96%9Cx%E5%92%8Cy%E8%BD%B4%EF%BC%8C%E4%BB%A5%EF%BC%88100%EF%BC%8C100%EF%BC%89%E4%B8%BA%E4%B8%AD%E5%BF%83%E3%80%82%0A%09%09%09mMatrix.postSkew(0.2f%2C%200.2f%2C%20100%2C%20100)%3B*%2F%0A%09%09%09%2F%2F%20pre%20%E6%96%B9%E5%BC%8F%EF%BC%9A%E5%89%8D%E4%B9%98%20%3E%3E%20%E5%8F%82%E6%95%B0%E7%BB%99%E5%87%BA%E7%9A%84%E7%9F%A9%E9%98%B5%E4%B9%98%E4%BB%A5%E5%BD%93%E5%89%8D%E7%9A%84%E7%9F%A9%E9%98%B5%E3%80%82%E6%89%80%E4%BB%A5%E6%93%8D%E4%BD%9C%E6%98%AF%E5%9C%A8%E5%BD%93%E5%89%8D%E7%9F%A9%E9%98%B5%E7%9A%84%E6%9C%80%E5%89%8D%E9%9D%A2%E5%8F%91%E7%94%9F%E7%9A%84%E3%80%82%0A%09%20%20%20%20%20%20%20%20%2F%2F%E5%B9%B3%E7%A7%BB%E5%88%B0%EF%BC%88100%EF%BC%8C100%EF%BC%89%E5%A4%84%0A%09%09%09mMatrix.preTranslate(100%2C%20100)%3B%0A%09%20%20%20%20%20%20%20%20%2F%2F%E5%80%BE%E6%96%9Cx%E5%92%8Cy%E8%BD%B4%EF%BC%8C%E4%BB%A5%EF%BC%88100%EF%BC%8C100%EF%BC%89%E4%B8%BA%E4%B8%AD%E5%BF%83%E3%80%82%0A%09%09%09mMatrix.preSkew(0.2f%2C%200.2f%2C%20100%2C%20100)%3B%0A%09%09%7D%0A%09%09%0A%09%09%40Override%20protected%20void%20onDraw(Canvas%20canvas)%20%7B%0A%2F%2F%09%09%09super.onDraw(canvas)%3B%20%20%2F%2F%E5%A6%82%E6%9E%9C%E7%95%8C%E9%9D%A2%E4%B8%8A%E8%BF%98%E6%9C%89%E5%85%B6%E4%BB%96%E5%85%83%E7%B4%A0%E9%9C%80%E8%A6%81%E7%BB%98%E5%88%B6%EF%BC%8C%E5%8F%AA%E9%9C%80%E8%A6%81%E5%B0%86%E8%BF%99%E5%8F%A5%E8%AF%9D%E5%86%99%E4%B8%8A%E5%B0%B1%E8%A1%8C%E4%BA%86%E3%80%82%0A%09%09%09Paint%20paint%20%3D%20new%20Paint(Paint.ANTI_ALIAS_FLAG)%3B%0A%09%09%09canvas.drawBitmap(mBitmap%2C%20mMatrix%2C%20paint)%3B%0A%09%09%7D%0A%09%7D%0A%09%0A%09%2F**%0A%09%20%20%09Matrix%E7%9A%84%E6%93%8D%E4%BD%9C%3A%0A%09%20%0A%09%20%09%E6%80%BB%E5%85%B1%E5%88%86%E4%B8%BAtranslate%EF%BC%88%E5%B9%B3%E7%A7%BB%EF%BC%89%EF%BC%8Crotate%EF%BC%88%E6%97%8B%E8%BD%AC%EF%BC%89%EF%BC%8Cscale%EF%BC%88%E7%BC%A9%E6%94%BE%EF%BC%89%E5%92%8Cskew%EF%BC%88%E5%80%BE%E6%96%9C%EF%BC%89%E5%9B%9B%E7%A7%8D%EF%BC%8C%E6%AF%8F%E4%B8%80%E7%A7%8D%E5%8F%98%E6%8D%A2%E5%9C%A8%0A%0A%09%09Android%E7%9A%84API%E9%87%8C%E9%83%BD%E6%8F%90%E4%BE%9B%E4%BA%86set%2C%20post%E5%92%8Cpre%E4%B8%89%E7%A7%8D%E6%93%8D%E4%BD%9C%E6%96%B9%E5%BC%8F%2C%E9%99%A4%E4%BA%86translate%EF%BC%8C%E5%85%B6%E4%BB%96%E4%B8%89%E7%A7%8D%E6%93%8D%E4%BD%9C%E9%83%BD%E5%8F%AF%E4%BB%A5%E6%8C%87%E5%AE%9A%E4%B8%AD%E5%BF%83%E7%82%B9%E3%80%82%20%0A%0A%20%20%20%20%09set%E6%98%AF%E7%9B%B4%E6%8E%A5%E8%AE%BE%E7%BD%AEMatrix%E7%9A%84%E5%80%BC%EF%BC%8C%E6%AF%8F%E6%AC%A1set%E4%B8%80%E6%AC%A1%EF%BC%8C%E6%95%B4%E4%B8%AAMatrix%E7%9A%84%E6%95%B0%E7%BB%84%E9%83%BD%E4%BC%9A%E5%8F%98%E6%8E%89%E3%80%82%20%0A%0A%20%20%20%20%09post%E6%98%AF%E5%90%8E%E4%B9%98%EF%BC%8C%E5%BD%93%E5%89%8D%E7%9A%84%E7%9F%A9%E9%98%B5%E4%B9%98%E4%BB%A5%E5%8F%82%E6%95%B0%E7%BB%99%E5%87%BA%E7%9A%84%E7%9F%A9%E9%98%B5%E3%80%82%E5%8F%AF%E4%BB%A5%E8%BF%9E%E7%BB%AD%E5%A4%9A%E6%AC%A1%E4%BD%BF%E7%94%A8post%EF%BC%8C%E6%9D%A5%E5%AE%8C%E6%88%90%E6%89%80%E9%9C%80%E7%9A%84%E6%95%B4%E4%B8%AA%E5%8F%98%E6%8D%A2%E3%80%82%0A%0A%20%09%09pre%E6%98%AF%E5%89%8D%E4%B9%98%EF%BC%8C%E5%8F%82%E6%95%B0%E7%BB%99%E5%87%BA%E7%9A%84%E7%9F%A9%E9%98%B5%E4%B9%98%E4%BB%A5%E5%BD%93%E5%89%8D%E7%9A%84%E7%9F%A9%E9%98%B5%E3%80%82%E6%89%80%E4%BB%A5%E6%93%8D%E4%BD%9C%E6%98%AF%E5%9C%A8%E5%BD%93%E5%89%8D%E7%9F%A9%E9%98%B5%E7%9A%84%E6%9C%80%E5%89%8D%E9%9D%A2%E5%8F%91%E7%94%9F%E7%9A%84%E3%80%82%0A%20%09%09%0A%09%20%09%E6%97%8B%E8%BD%AC%E3%80%81%E7%BC%A9%E6%94%BE%E5%92%8C%E5%80%BE%E6%96%9C%E9%83%BD%E5%8F%AF%E4%BB%A5%E5%9B%B4%E7%BB%95%E4%B8%80%E4%B8%AA%E4%B8%AD%E5%BF%83%E7%82%B9%E6%9D%A5%E8%BF%9B%E8%A1%8C%EF%BC%8C%E5%A6%82%E6%9E%9C%E4%B8%8D%E6%8C%87%E5%AE%9A%EF%BC%8C%E9%BB%98%E8%AE%A4%E6%83%85%E5%86%B5%E4%B8%8B%EF%BC%8C%E6%98%AF%E5%9B%B4%E7%BB%95(0%2C0)%E7%82%B9%E6%9D%A5%E8%BF%9B%E8%A1%8C%E3%80%82%0A%09%20*%2F" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="15" width="14">&nbsp;<a href="javascript:void()" title="收藏这段代码" onclick="code_favorites_do_favorite(this);return false;"><img class="star" src="1058517_files/icon_star.png" alt="收藏代码"><img class="spinner" src="1058517_files/spinner.gif" style="display:none"></a></div></div><ol class="dp-j" start="1"><li><span><span class="keyword">public</span><span>&nbsp;</span><span class="keyword">class</span><span>&nbsp;Matrix2Draw&nbsp;</span><span class="keyword">extends</span><span>&nbsp;View&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">private</span><span>&nbsp;Bitmap&nbsp;mBitmap;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">private</span><span>&nbsp;Matrix&nbsp;mMatrix&nbsp;=&nbsp;</span><span class="keyword">new</span><span>&nbsp;Matrix();&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;Matrix2Draw(Context&nbsp;context)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">super</span><span>(context);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;initialize();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">private</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;initialize()&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Bitmap&nbsp;bmp&nbsp;=&nbsp;((BitmapDrawable)getResources().getDrawable(R.drawable.rotate_surfaceview)).getBitmap();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mBitmap&nbsp;=&nbsp;bmp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*首先，将缩放为100*100。这里scale的参数是比例。有一点要注意，如果直接用100/</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bmp.getWidth()
的话，会得到0，因为是整型相除，所以必须其中有一个是float型的，</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;直接用100f就好。*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mMatrix.setScale(100f/bmp.getWidth(),&nbsp;100f/bmp.getHeight());&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*//&nbsp;post&nbsp;方式：后乘&nbsp;&gt;&gt;&nbsp;当前的矩阵乘以参数给出的矩阵。可以连续多次使用post，来完成所需的整个变换。</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//平移到（100，100）处</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mMatrix.postTranslate(100,&nbsp;100);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//倾斜x和y轴，以（100，100）为中心。</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mMatrix.postSkew(0.2f,&nbsp;0.2f,&nbsp;100,&nbsp;100);*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;pre&nbsp;方式：前乘&nbsp;&gt;&gt;&nbsp;参数给出的矩阵乘以当前的矩阵。所以操作是在当前矩阵的最前面发生的。</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//平移到（100，100）处</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mMatrix.preTranslate(<span class="number">100</span><span>,&nbsp;</span><span class="number">100</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//倾斜x和y轴，以（100，100）为中心。</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mMatrix.preSkew(<span class="number">0</span><span>.2f,&nbsp;</span><span class="number">0</span><span>.2f,&nbsp;</span><span class="number">100</span><span>,&nbsp;</span><span class="number">100</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="annotation">@Override</span><span>&nbsp;</span><span class="keyword">protected</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;onDraw(Canvas&nbsp;canvas)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span><span class="comment">//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super.onDraw(canvas);&nbsp;&nbsp;//如果界面上还有其他元素需要绘制，只需要将这句话写上就行了。</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Paint&nbsp;paint&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Paint(Paint.ANTI_ALIAS_FLAG);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawBitmap(mBitmap,&nbsp;mMatrix,&nbsp;paint);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/**</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Matrix的操作:</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;总共分为translate（平移），rotate（旋转），scale（缩放）和skew（倾斜）四种，每一种变换在</span>&nbsp;</span></li><li><span><span class="comment"></span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Android的API里都提供了set,&nbsp;post和pre三种操作方式,除了translate，其他三种操作都可以指定中心点。&nbsp;</span>&nbsp;</span></li><li><span><span class="comment"></span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set是直接设置Matrix的值，每次set一次，整个Matrix的数组都会变掉。&nbsp;</span>&nbsp;</span></li><li><span><span class="comment"></span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;post是后乘，当前的矩阵乘以参数给出的矩阵。可以连续多次使用post，来完成所需的整个变换。</span>&nbsp;</span></li><li><span><span class="comment"></span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pre是前乘，参数给出的矩阵乘以当前的矩阵。所以操作是在当前矩阵的最前面发生的。</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;旋转、缩放和倾斜都可以围绕一个中心点来进行，如果不指定，默认情况下，是围绕(0,0)点来进行。</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li></ol></div><pre title="Android 图形用户界面 之 绘图（一）" pre_index="4" source_url="http://emmet1988.iteye.com/blog/1058517" codeable_type="Blog" codeable_id="1058517" style="display: none;" class="java" name="code">public class Matrix2Draw extends View {
		
		private Bitmap mBitmap;
		private Matrix mMatrix = new Matrix();
		
		public Matrix2Draw(Context context) {
			super(context);
			initialize();
		}

		private void initialize() {
			
			Bitmap bmp = ((BitmapDrawable)getResources().getDrawable(R.drawable.rotate_surfaceview)).getBitmap();
			mBitmap = bmp;
			/*首先，将缩放为100*100。这里scale的参数是比例。有一点要注意，如果直接用100/
			   bmp.getWidth()的话，会得到0，因为是整型相除，所以必须其中有一个是float型的，
			   直接用100f就好。*/
			mMatrix.setScale(100f/bmp.getWidth(), 100f/bmp.getHeight());
			/*// post 方式：后乘 &gt;&gt; 当前的矩阵乘以参数给出的矩阵。可以连续多次使用post，来完成所需的整个变换。
	        //平移到（100，100）处
			mMatrix.postTranslate(100, 100);
	        //倾斜x和y轴，以（100，100）为中心。
			mMatrix.postSkew(0.2f, 0.2f, 100, 100);*/
			// pre 方式：前乘 &gt;&gt; 参数给出的矩阵乘以当前的矩阵。所以操作是在当前矩阵的最前面发生的。
	        //平移到（100，100）处
			mMatrix.preTranslate(100, 100);
	        //倾斜x和y轴，以（100，100）为中心。
			mMatrix.preSkew(0.2f, 0.2f, 100, 100);
		}
		
		@Override protected void onDraw(Canvas canvas) {
//			super.onDraw(canvas);  //如果界面上还有其他元素需要绘制，只需要将这句话写上就行了。
			Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
			canvas.drawBitmap(mBitmap, mMatrix, paint);
		}
	}
	
	/**
	  	Matrix的操作:
	 
	 	总共分为translate（平移），rotate（旋转），scale（缩放）和skew（倾斜）四种，每一种变换在

		Android的API里都提供了set, post和pre三种操作方式,除了translate，其他三种操作都可以指定中心点。 

    	set是直接设置Matrix的值，每次set一次，整个Matrix的数组都会变掉。 

    	post是后乘，当前的矩阵乘以参数给出的矩阵。可以连续多次使用post，来完成所需的整个变换。

 		pre是前乘，参数给出的矩阵乘以当前的矩阵。所以操作是在当前矩阵的最前面发生的。
 		
	 	旋转、缩放和倾斜都可以围绕一个中心点来进行，如果不指定，默认情况下，是围绕(0,0)点来进行。
	 */</pre>
<p>&nbsp;</p>
<p>SimpleDraw.java</p>
<p>&nbsp;</p>
<div id="" class="dp-highlighter"><div class="bar"><div class="tools">Java代码 <embed wmode="transparent" src="1058517_files/clipboard_new.swf" flashvars="clipboard=package%20com.view%3B%0A%0Aimport%20android.content.Context%3B%0Aimport%20android.graphics.Canvas%3B%0Aimport%20android.graphics.Color%3B%0Aimport%20android.graphics.Paint%3B%0Aimport%20android.graphics.Rect%3B%0Aimport%20android.view.View%3B%0A%0Apublic%20class%20SimpleDraw%20extends%20View%20implements%20Runnable%20%7B%0A%0A%09%2F*%0A%09%20*%20%E6%88%91%E4%BB%AC%E7%BB%A7%E7%BB%AD%E6%9D%A5%E4%BB%8B%E7%BB%8DAndroid%E5%B9%B3%E5%8F%B0%E5%BA%95%E5%B1%82%E7%BB%98%E5%9B%BE%E7%B1%BB%E7%9A%84%E7%9B%B8%E5%85%B3%E5%86%85%E5%AE%B9%EF%BC%8C%E5%9C%A8Android%20UI%E5%BC%80%E5%8F%91%E4%B8%93%E9%A2%98(%E4%B8%80)%0A%09%20*%20%E4%B9%8B%E7%95%8C%E9%9D%A2%E8%AE%BE%E8%AE%A1%E4%B8%AD%E6%88%91%E4%BB%AC%E4%BB%8B%E7%BB%8D%E4%BA%86%E6%9C%89%E5%85%B3Android%E5%B9%B3%E5%8F%B0%E8%B5%84%E6%BA%90%E4%BD%BF%E7%94%A8%E4%BB%A5%E5%8F%8ABitmap%E7%9B%B8%E5%85%B3%E7%B1%BB%E7%9A%84%E6%93%8D%E4%BD%9C%0A%09%20*%20%EF%BC%8C%E6%8E%A5%E4%B8%8B%E6%9D%A5%E5%B0%86%E4%BC%9A%E4%BB%A5%E5%AE%9E%E4%BE%8B%E7%9A%84%E6%96%B9%E5%BC%8F%E7%BB%99%E5%A4%A7%E5%AE%B6%E6%BC%94%E7%A4%BA%E5%90%84%E7%A7%8D%E7%B1%BB%E7%9A%84%E7%94%A8%E5%A4%84%E4%BB%A5%E5%8F%8A%E6%B3%A8%E6%84%8F%E7%82%B9%E3%80%82%E4%BB%8A%E5%A4%A9%E6%88%91%E4%BB%AC%E7%BB%A7%E7%BB%AD%0A%09%20*%20%E4%BA%86%E8%A7%A3android.graphics%E5%8C%85%E4%B8%AD%E6%AF%94%E8%BE%83%E9%87%8D%E8%A6%81%E7%9A%84%E7%BB%98%E5%9B%BE%E7%B1%BB%E3%80%82%0A%09%20*%20%0A%09%20*%20%E4%B8%80%E3%80%81%20android.graphics.Matrix%E6%9C%89%E5%85%B3%E5%9B%BE%E5%BD%A2%E7%9A%84%E5%8F%98%E6%8D%A2%E3%80%81%E7%BC%A9%E6%94%BE%E7%AD%89%E7%9B%B8%E5%85%B3%E6%93%8D%E4%BD%9C%E5%B8%B8%E7%94%A8%E7%9A%84%E6%96%B9%E6%B3%95%E6%9C%89%3A%0A%09%20*%20Java%E4%BB%A3%E7%A0%81%EF%BC%9A%20void%20reset()%20%2F%2F%20%E9%87%8D%E7%BD%AE%E4%B8%80%E4%B8%AAmatrix%E5%AF%B9%E8%B1%A1%E3%80%82%0A%09%20*%20void%20set(Matrix%20src)%20%2F%2F%E5%A4%8D%E5%88%B6%E4%B8%80%E4%B8%AA%E6%BA%90%E7%9F%A9%E9%98%B5%EF%BC%8C%E5%92%8C%E6%9C%AC%E7%B1%BB%E7%9A%84%E6%9E%84%E9%80%A0%E6%96%B9%E6%B3%95%20Matrix(Matrix%20src)%20%E4%B8%80%E6%A0%B7%0A%09%20*%20boolean%20isIdentity()%20%2F%2F%E8%BF%94%E5%9B%9E%E8%BF%99%E4%B8%AA%E7%9F%A9%E9%98%B5%E6%98%AF%E5%90%A6%E5%AE%9A%E4%B9%89(%E5%B7%B2%E7%BB%8F%E6%9C%89%E6%84%8F%E4%B9%89)%0A%09%20*%20void%20setRotate(float%20degrees)%20%2F%2F%E6%8C%87%E5%AE%9A%E4%B8%80%E4%B8%AA%E8%A7%92%E5%BA%A6%E4%BB%A50%2C0%E4%B8%BA%E5%9D%90%E6%A0%87%E8%BF%9B%E8%A1%8C%E6%97%8B%E8%BD%AC%0A%09%20*%20void%20setRotate(float%20degrees%2C%20float%20px%2C%20float%20py)%20%2F%2F%E6%8C%87%E5%AE%9A%E4%B8%80%E4%B8%AA%E8%A7%92%E5%BA%A6%E4%BB%A5px%2Cpy%E4%B8%BA%E5%9D%90%E6%A0%87%E8%BF%9B%E8%A1%8C%E6%97%8B%E8%BD%AC%0A%09%20*%20%20void%20setScale(float%20sx%2C%20float%20sy)%20%2F%2F%20%E7%BC%A9%E6%94%BE%0A%09%20*%20void%20setScale(float%20sx%2C%20float%20sy%2C%20float%20px%2C%20float%20py)%20%2F%2F%E4%BB%A5%E5%9D%90%E6%A0%87px%2Cpy%E8%BF%9B%E8%A1%8C%E7%BC%A9%E6%94%BE%0A%09%20*%20void%20setTranslate(float%20dx%2C%20float%20dy)%20%2F%2F%E5%B9%B3%E7%A7%BB%0A%09%20*%20void%20setSkew%20(float%20kx%2C%20float%20ky%2C%20float%20px%2C%20float%20py)%20%2F%2F%E4%BB%A5%E5%9D%90%E6%A0%87px%2Cpy%E8%BF%9B%E8%A1%8C%E5%80%BE%E6%96%9C%0A%09%20*%20void%20setSkew%20(float%20kx%2C%20float%20ky)%20%2F%2F%E5%80%BE%E6%96%9C%20%E5%A4%8D%E5%88%B6%E4%BB%A3%E7%A0%81%20%E4%BA%8C%E3%80%81android.graphics.NinePatch%0A%09%20*%20%0A%09%20*%20NinePatch%E6%98%AFAndroid%E5%B9%B3%E5%8F%B0%E7%89%B9%E6%9C%89%E7%9A%84%E4%B8%80%E7%A7%8D%E9%9D%9E%E7%9F%A2%E9%87%8F%E5%9B%BE%E5%BD%A2%E8%87%AA%E7%84%B6%E6%8B%89%E4%BC%B8%E5%A4%84%E7%90%86%E6%96%B9%E6%B3%95%EF%BC%8C%E5%8F%AF%E4%BB%A5%E5%B8%AE%E5%8A%A9%E5%B8%B8%E8%A7%84%E7%9A%84%0A%09%20*%20%E5%9B%BE%E5%BD%A2%E5%9C%A8%E6%8B%89%E4%BC%B8%E6%97%B6%E4%B8%8D%E4%BC%9A%E7%BC%A9%E6%94%BE%EF%BC%8C%0A%09%20*%20%E5%AE%9E%E4%BE%8B%E4%B8%ADAndroid%E5%BC%80%E5%8F%91%E7%BD%91%E6%8F%90%E7%A4%BA%E5%A4%A7%E5%AE%B6%E5%AF%B9%E4%BA%8EToast%E7%9A%84%E6%98%BE%E7%A4%BA%E5%B0%B1%E6%98%AF%E8%AF%A5%E5%8E%9F%E7%90%86%EF%BC%8C%E5%90%8C%E6%97%B6SDK%E4%B8%AD%E6%8F%90%E4%BE%9B%E4%BA%86%E4%B8%80%0A%09%20*%20%E4%B8%AA%E5%B7%A5%E5%85%B7%E5%90%8D%E4%B8%BADraw%0A%09%20*%209-Patch%EF%BC%8C%E6%9C%89%E5%85%B3%E8%AF%A5%E5%B7%A5%E5%85%B7%E7%9A%84%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95%E5%8F%AF%E4%BB%A5%E5%8F%82%E8%80%83%E6%88%91%E4%BB%AC%E7%BB%8F%E5%8F%91%E5%B8%83%E7%9A%84%20Draw%0A%09%20*%209-Patch%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95%E4%BB%8B%E7%BB%8D%E4%B8%80%E6%96%87%E3%80%82%E7%94%B1%E4%BA%8E%E8%AF%A5%E7%B1%BB%E6%8F%90%E4%BE%9B%E4%BA%86%E9%AB%98%E8%B4%A8%E9%87%8F%E6%94%AF%E6%8C%81%E9%80%8F%E6%98%8E%E7%9A%84%E7%BC%A9%E6%94%BE%E6%96%B9%E5%BC%8F%EF%BC%8C%E6%89%80%E4%BB%A5%E5%9B%BE%E5%BD%A2%0A%09%20*%20%E6%A0%BC%E5%BC%8F%E4%B8%BAPNG%EF%BC%8C%E6%96%87%E4%BB%B6%E5%91%BD%E5%90%8D%E6%96%B9%E5%BC%8F%E4%B8%BA.9.png%0A%09%20*%20%E7%9A%84%E5%90%8E%E7%BC%80%E6%AF%94%E5%A6%82eoeandroid%E3%80%82%0A%09%20*%20%0A%09%20*%20%E4%B8%89%E3%80%81android.graphics.Paint%E3%80%82Paint%E7%B1%BB%E6%88%91%E4%BB%AC%E5%8F%AF%E4%BB%A5%E7%90%86%E8%A7%A3%E4%B8%BA%E7%94%BB%E7%AC%94%E3%80%81%E7%94%BB%E5%88%B7%E7%9A%84%E5%B1%9E%E6%80%A7%E5%AE%9A%E4%B9%89%EF%BC%8C%E6%9C%AC%E7%B1%BB%0A%09%20*%20%E5%B8%B8%E7%94%A8%E7%9A%84%E6%96%B9%E6%B3%95%E5%A6%82%E4%B8%8B%3A%0A%09%20*%20%0A%09%20*%20Java%E4%BB%A3%E7%A0%81%EF%BC%9A%20void%20reset()%20%2F%2F%E9%87%8D%E7%BD%AE%0A%09%20*%20void%20setARGB(int%20a%2C%20int%20r%2C%20int%20g%2C%20int%20b)%20%E6%88%96%20void%20setColor(int%20color)%0A%09%20*%20%2F%2F%E5%9D%87%E4%B8%BA%E8%AE%BE%E7%BD%AEPaint%E5%AF%B9%E8%B1%A1%E7%9A%84%E9%A2%9C%E8%89%B2%0A%09%20*%20void%20setAntiAlias(boolean%20aa)%0A%09%20*%20%0A%09%20*%20%2F%2F%E6%98%AF%E5%90%A6%E6%8A%97%E9%94%AF%E9%BD%BF%EF%BC%8C%E9%9C%80%E8%A6%81%E9%85%8D%E5%90%88void%20setFlags%20(Paint.ANTI_ALIAS_FLAG)%20%E6%9D%A5%E5%B8%AE%E5%8A%A9%E6%B6%88%E9%99%A4%E9%94%AF%E9%BD%BF%0A%09%20*%20%E4%BD%BF%E5%85%B6%E8%BE%B9%E7%BC%98%E6%9B%B4%E5%B9%B3%E6%BB%91%E3%80%82%0A%09%20*%20Shader%20setShader(Shader%20shader)%0A%09%20*%20%0A%09%20*%20%2F%2F%E8%AE%BE%E7%BD%AE%E9%98%B4%E5%BD%B1%EF%BC%8CShader%E7%B1%BB%E6%98%AF%E4%B8%80%E4%B8%AA%E7%9F%A9%E9%98%B5%E5%AF%B9%E8%B1%A1%EF%BC%8C%E5%A6%82%E6%9E%9C%E4%B8%BANULL%E5%B0%86%E6%B8%85%E9%99%A4%E9%98%B4%E5%BD%B1%E3%80%82%0A%09%20*%20void%20setStyle(Paint.Style%20style)%20%2F%2F%E8%AE%BE%E7%BD%AE%E6%A0%B7%E5%BC%8F%EF%BC%8C%E4%B8%80%E8%88%AC%E4%B8%BA%20FILL%20%E5%A1%AB%E5%85%85%EF%BC%8C%E6%88%96%E8%80%85STROKE%E5%87%B9%E9%99%B7%0A%09%20*%20%E6%95%88%E6%9E%9C%E3%80%82%0A%09%20*%20void%20setTextSize(float%20textSize)%20%2F%2F%E8%AE%BE%E7%BD%AE%E5%AD%97%E4%BD%93%E5%A4%A7%E5%B0%8F%0A%09%20*%20void%20setTextAlign(Paint.Align%20align)%20%2F%2F%E6%96%87%E6%9C%AC%E5%AF%B9%E9%BD%90%E6%96%B9%E5%BC%8F%0A%09%20*%20Typeface%20setTypeface(Typeface%20typeface)%0A%09%20*%20%2F%2F%E8%AE%BE%E7%BD%AE%E5%AD%97%E4%BD%93%EF%BC%8C%E9%80%9A%E8%BF%87Typeface%E5%8F%AF%E4%BB%A5%E5%8A%A0%E8%BD%BDAndroid%E5%86%85%E9%83%A8%E7%9A%84%E5%AD%97%E4%BD%93%EF%BC%8C%E4%B8%80%E8%88%AC%E4%B8%BA%E5%AE%8B%E4%BD%93%E5%AF%B9%E4%BA%8E%E4%B8%AD%E6%96%87%EF%BC%8C%0A%09%20*%20%E9%83%A8%E5%88%86ROM%E5%8F%AF%E4%BB%A5%E8%87%AA%E5%B7%B1%E6%B7%BB%E5%8A%A0%E6%AF%94%E5%A6%82%E9%9B%85%E9%BB%91%E7%AD%89%E7%AD%89%0A%09%20*%20void%20setUnderlineText(boolean%20underlineText)%0A%09%20*%20%0A%09%20*%20%2F%2F%E6%98%AF%E5%90%A6%E8%AE%BE%E7%BD%AE%E4%B8%8B%E5%88%92%E7%BA%BF%EF%BC%8C%E9%9C%80%E8%A6%81%E6%92%87%E5%92%8Cvoid%20setFlags%20(Paint.UNDERLINE_TEXT_FLAG)%20%E6%96%B9%E6%B3%95%E3%80%82%20%E5%A4%8D%E5%88%B6%E4%BB%A3%E7%A0%81%0A%09%20*%20%E5%9B%9B%E3%80%81android.graphics.Rect%0A%09%20*%20%0A%09%20*%20Rect%E6%88%91%E4%BB%AC%E5%8F%AF%E4%BB%A5%E7%90%86%E8%A7%A3%E4%B8%BA%E7%9F%A9%E5%BD%A2%E5%8C%BA%E5%9F%9F%EF%BC%8C%E7%B1%BB%E4%BC%BC%E7%9A%84%E8%BF%98%E6%9C%89Point%E4%B8%80%E4%B8%AA%E7%82%B9%EF%BC%8CRect%E7%B1%BB%E9%99%A4%E4%BA%86%E8%A1%A8%E7%A4%BA%E4%B8%80%E4%B8%AA%E7%9F%A9%0A%09%20*%20%E5%BD%A2%E5%8C%BA%E5%9F%9F%E4%BD%8D%E7%BD%AE%E6%8F%8F%E8%BF%B0%E5%A4%96%EF%BC%8C%0A%09%20*%20eoeandroid%E6%8F%90%E7%A4%BA%E4%B8%BB%E8%A6%81%E5%8F%AF%E4%BB%A5%E5%B8%AE%E5%8A%A9%E6%88%91%E4%BB%AC%E8%AE%A1%E7%AE%97%E5%9B%BE%E5%BD%A2%E4%B9%8B%E9%97%B4%E6%98%AF%E5%90%A6%E7%A2%B0%E6%92%9E%0A%09%20*%20(%E5%8C%85%E5%90%AB)%E5%85%B3%E7%B3%BB%EF%BC%8C%E5%AF%B9%E4%BA%8EAndroid%E6%B8%B8%E6%88%8F%E5%BC%80%E5%8F%91%E6%AF%94%E8%BE%83%E6%9C%89%E7%94%A8%EF%BC%8C%E5%85%B6%E4%B8%BB%E8%A6%81%E7%9A%84%E6%88%90%E5%91%98contains%E5%8C%85%E5%90%AB%E4%BA%86%E4%B8%89%E7%A7%8D%0A%09%20*%20%E9%87%8D%E8%BD%BD%E6%96%B9%E6%B3%95%EF%BC%8C%E6%9D%A5%E5%88%A4%E6%96%AD%E5%8C%85%E5%90%AB%E5%85%B3%E7%B3%BB.%0A%09%20*%20%0A%09%20*%20Java%E4%BB%A3%E7%A0%81%EF%BC%9A%20%0A%09%20*%20boolean%20contains(int%20left%2C%20int%20top%2C%20int%20right%2C%20int%20bottom)%0A%09%20*%20boolean%20contains(int%20x%2C%20int%20y)%0A%09%20*%20%0A%09%20*%20boolean%20contains(Rect%20r)%20%E5%A4%8D%E5%88%B6%E4%BB%A3%E7%A0%81%20%E4%BA%94%E3%80%81android.graphics.Region%0A%09%20*%20Region%E5%9C%A8Android%E5%B9%B3%E5%8F%B0%E4%B8%AD%E8%A1%A8%E7%A4%BA%E4%B8%80%E4%B8%AA%E5%8C%BA%E5%9F%9F%E5%92%8CRect%E4%B8%8D%E5%90%8C%E7%9A%84%E6%98%AF%0A%09%20*%20%EF%BC%8C%E5%AE%83%E8%A1%A8%E7%A4%BA%E7%9A%84%E6%98%AF%E4%B8%80%E4%B8%AA%E4%B8%8D%E8%A7%84%E5%88%99%E7%9A%84%E6%A0%B7%E5%AD%90%EF%BC%8C%E5%8F%AF%E4%BB%A5%E6%98%AF%E6%A4%AD%E5%9C%86%E3%80%81%E5%A4%9A%E8%BE%B9%E5%BD%A2%E7%AD%89%E7%AD%89%EF%BC%8C%E8%80%8CRect%E4%BB%85%E4%BB%85%E6%98%AF%E7%9F%A9%E5%BD%A2%E3%80%82%0A%09%20*%20%E5%90%8C%E6%A0%B7Region%E7%9A%84boolean%20contains(int%20x%2C%0A%09%20*%20int%20y)%20%E6%88%90%E5%91%98%E5%8F%AF%E4%BB%A5%E5%88%A4%E6%96%AD%E4%B8%80%E4%B8%AA%E7%82%B9%E6%98%AF%E5%90%A6%E5%9C%A8%E8%AF%A5%E5%8C%BA%E5%9F%9F%E5%86%85%E3%80%82%0A%09%20*%20%0A%09%20*%20%E5%85%AD%E3%80%81android.graphics.Typeface%0A%09%20*%20Typeface%E7%B1%BB%E6%98%AF%E5%B8%AE%E5%8A%A9%E6%8F%8F%E8%BF%B0%E4%B8%80%E4%B8%AA%E5%AD%97%E4%BD%93%E5%AF%B9%E8%B1%A1%EF%BC%8C%E5%9C%A8TextView%E4%B8%AD%E9%80%9A%E8%BF%87%E4%BD%BF%E7%94%A8setTypeface%E6%96%B9%E6%B3%95%E6%9D%A5%0A%09%20*%20%E5%88%B6%E5%AE%9A%E4%B8%80%E4%B8%AA%E8%BE%93%E5%87%BA%E6%96%87%E6%9C%AC%E7%9A%84%E5%AD%97%E4%BD%93%0A%09%20*%20%EF%BC%8C%E5%85%B6%E7%9B%B4%E6%8E%A5%E6%9E%84%E9%80%A0%E8%B0%83%E7%94%A8%E6%88%90%E5%91%98create%E6%96%B9%E6%B3%95%E5%8F%AF%E4%BB%A5%E7%9B%B4%E6%8E%A5%E6%8C%87%E5%AE%9A%E4%B8%80%E4%B8%AA%E5%AD%97%E4%BD%93%E5%90%8D%E7%A7%B0%E5%92%8C%E6%A0%B7%E5%BC%8F%EF%BC%8C%E6%AF%94%E5%A6%82%0A%09%20*%20Java%E4%BB%A3%E7%A0%81%EF%BC%9A%20static%20Typeface%20create(Typeface%20family%2C%20int%20style)%0A%09%20*%20%0A%09%20*%20static%20Typeface%20create(String%20familyName%2C%20int%20style)%20%E5%A4%8D%E5%88%B6%E4%BB%A3%E7%A0%81%0A%09%20*%20%E5%90%8C%E6%97%B6%E4%BD%BF%E7%94%A8isBold%E5%92%8CisItalic%E6%96%B9%E6%B3%95%E5%8F%AF%E4%BB%A5%E5%88%A4%E6%96%AD%E5%87%BA%E6%98%AF%E5%90%A6%E5%8C%85%E5%90%AB%E7%B2%97%E4%BD%93%E6%88%96%E6%96%9C%E4%BD%93%E7%9A%84%E5%AD%97%E5%9E%8B%E3%80%82%0A%09%20*%20%0A%09%20*%20Java%E4%BB%A3%E7%A0%81%EF%BC%9A%20final%20boolean%20isBold()%0A%09%20*%20final%20boolean%20isItalic()%20%E5%A4%8D%E5%88%B6%E4%BB%A3%E7%A0%81%20%E8%AF%A5%E7%B1%BB%E7%9A%84%E5%88%9B%E5%BB%BA%E6%96%B9%E6%B3%95%E8%BF%98%E6%9C%89%E4%BB%8Eapk%E7%9A%84%E8%B5%84%E6%BA%90%E6%88%96%E4%BB%8E%E4%B8%80%E4%B8%AA%E5%85%B7%E4%BD%93%E7%9A%84%0A%09%20*%20%E6%96%87%E4%BB%B6%E8%B7%AF%E5%BE%84%EF%BC%8C%E5%85%B6%E5%85%B7%E4%BD%93%E6%96%B9%E6%B3%95%E4%B8%BA%0A%09%20*%20Java%E4%BB%A3%E7%A0%81%EF%BC%9A%20static%20Typeface%20createFromAsset(AssetManager%20mgr%2C%20String%20path)%0A%09%20*%20static%20Typeface%20createFromFile(File%20path)%0A%09%20*%20static%20Typeface%20createFromFile(String%20path)%20%E5%A4%8D%E5%88%B6%E4%BB%A3%E7%A0%81%0A%09%20*%2F%0A%0A%09private%20Paint%20paint%3B%0A%0A%09public%20SimpleDraw(Context%20context)%20%7B%0A%09%09super(context)%3B%0A%09%09paint%20%3D%20new%20Paint()%3B%0A%09%09new%20Thread(this).start()%3B%0A%09%7D%0A%0A%09%40Override%0A%09protected%20void%20onDraw(Canvas%20canvas)%20%7B%0A%09%09super.onDraw(canvas)%3B%0A%0A%09%09canvas.drawColor(Color.GRAY)%3B%2F%2F%0A%09%09paint.setAntiAlias(true)%3B%2F%2F%0A%0A%09%09canvas.save()%3B%2F%2F%0A%09%09canvas.clipRect(10%2C%2010%2C%20110%2C%20110)%3B%2F%2F%0A%09%09canvas.drawColor(Color.WHITE)%3B%2F%2F%0A%09%09%2F%2F%20canvas.rotate(m)%3B%2F%2F%E4%BB%A5%E5%B1%8F%E5%B9%95%E5%B7%A6%E4%B8%8A%E8%A7%92%E4%B8%BA%E5%9D%90%E6%A0%87%E5%8E%9F%E7%82%B9%E6%97%8B%E8%BD%AC%0A%09%09m%20%2B%3D%2045.0f%3B%0A%09%09if%20(m%20%3D%3D%20360.0f)%20%7B%0A%09%09%09m%20%3D%200.0f%3B%0A%09%09%7D%0A%09%09canvas.rotate(m%2C%2060%2C%2060)%3B%2F%2F%20%E4%BB%A5(60%2C60)%E4%B8%BA%E5%8E%9F%E7%82%B9%E6%97%8B%E8%BD%AC%0A%09%09paint.setColor(Color.GREEN)%3B%0A%09%09canvas.drawRect(new%20Rect(50%2C%2050%2C%2070%2C%2070)%2C%20paint)%3B%0A%09%09canvas.restore()%3B%0A%0A%09%09canvas.save()%3B%0A%09%09canvas.translate(140%2C%2010)%3B%0A%09%09canvas.clipRect(0%2C%200%2C%20100%2C%20100)%3B%2F%2F%20%E4%B8%80%E5%AE%9A%E8%A6%81%E5%85%88%E5%89%AA%E8%BE%91%E5%87%BA%E7%9F%A9%E5%BD%A2%E5%8C%BA%E5%9F%9F%E5%86%8D%E8%AE%BE%E7%94%BB%E5%B8%83%E8%83%8C%E6%99%AF%EF%BC%8C%0A%09%09%2F%2F%E5%90%A6%E5%88%99%E4%BC%9A%E8%A6%86%E7%9B%96%E6%95%B4%E5%BC%A0%E7%94%BB%E5%B8%83%0A%09%09canvas.drawColor(Color.BLACK)%3B%0A%09%09paint.setColor(Color.BLUE)%3B%0A%09%09canvas.drawRect(new%20Rect(10%2C%2010%2C%2050%2C%2050)%2C%20paint)%3B%0A%09%09canvas.restore()%3B%0A%09%09%2F%2F%0A%09%09canvas.save()%3B%0A%09%09canvas.translate(120%2C%20120)%3B%0A%09%09canvas.clipRect(new%20Rect(0%2C%200%2C%20100%2C%20100))%3B%0A%09%09canvas.drawColor(Color.GREEN)%3B%0A%09%09%2F%2F%20paint.setColor(Color.BLUE)%3B%0A%09%09paint.setStrokeWidth(4)%3B%0A%09%09paint.setColor(Color.BLACK)%3B%0A%09%09canvas.drawLine(0%2C%2060%2C%20100%2C%2060%2C%20paint)%3B%0A%09%09paint.setARGB(255%2C%2051%2C%2051%2C%2051)%3B%0A%09%09paint.setTextSize(20)%3B%0A%09%09paint.setFlags(Paint.ANTI_ALIAS_FLAG)%3B%0A%09%09paint.setUnderlineText(true)%3B%0A%09%09%2F%2F%20paint.setFlags(Paint.UNDERLINE_TEXT_FLAG)%3B%0A%09%09canvas.drawText(%22%E9%99%88%E5%BB%BA%E7%AB%8B%22%2C%2025%2C%2080%2C%20paint)%3B%0A%09%09paint.setColor(Color.WHITE)%3B%0A%09%09canvas.drawRect(new%20Rect(10%2C%2010%2C%2050%2C%2050)%2C%20paint)%3B%0A%09%09canvas.restore()%3B%0A%09%7D%0A%0A%09float%20m%20%3D%200.0f%3B%0A%0A%09public%20void%20run()%20%7B%0A%09%09while%20(!Thread.currentThread().isInterrupted())%20%7B%0A%09%09%09try%20%7B%0A%09%09%09%09Thread.sleep(500)%3B%2F%2F%20%E6%AF%8F%E5%8D%8A%E7%A7%92%E6%89%A7%E8%A1%8C%E4%B8%80%E6%AC%A1%0A%09%09%09%7D%20catch%20(InterruptedException%20e)%20%7B%0A%09%09%09%09Thread.currentThread().interrupt()%3B%0A%09%09%09%7D%0A%09%09%09postInvalidate()%3B%0A%09%09%7D%0A%09%7D%0A%7D%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="15" width="14">&nbsp;<a href="javascript:void()" title="收藏这段代码" onclick="code_favorites_do_favorite(this);return false;"><img class="star" src="1058517_files/icon_star.png" alt="收藏代码"><img class="spinner" src="1058517_files/spinner.gif" style="display:none"></a></div></div><ol class="dp-j" start="1"><li><span><span class="keyword">package</span><span>&nbsp;com.view;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span><span class="keyword">import</span><span>&nbsp;android.content.Context;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.Canvas;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.Color;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.Paint;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.Rect;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.view.View;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span><span class="keyword">public</span><span>&nbsp;</span><span class="keyword">class</span><span>&nbsp;SimpleDraw&nbsp;</span><span class="keyword">extends</span><span>&nbsp;View&nbsp;</span><span class="keyword">implements</span><span>&nbsp;Runnable&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;我们继续来介绍Android平台底层绘图类的相关内容，在Android&nbsp;UI开发专题(一)</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;之界面设计中我们介绍了有关Android平台资源使用以及Bitmap相关类的操作</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;，接下来将会以实例的方式给大家演示各种类的用处以及注意点。今天我们继续</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;了解android.graphics包中比较重要的绘图类。</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;一、&nbsp;android.graphics.Matrix有关图形的变换、缩放等相关操作常用的方法有:</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Java代码：&nbsp;void&nbsp;reset()&nbsp;//&nbsp;重置一个matrix对象。</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;void&nbsp;set(Matrix&nbsp;src)&nbsp;//复制一个源矩阵，和本类的构造方法&nbsp;Matrix(Matrix&nbsp;src)&nbsp;一样</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;boolean&nbsp;isIdentity()&nbsp;//返回这个矩阵是否定义(已经有意义)</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;void&nbsp;setRotate(float&nbsp;degrees)&nbsp;//指定一个角度以0,0为坐标进行旋转</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;void&nbsp;setRotate(float&nbsp;degrees,&nbsp;float&nbsp;px,&nbsp;float&nbsp;py)&nbsp;//
指定一个角度以px,py为坐标进行旋转</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;void&nbsp;setScale(float&nbsp;sx,&nbsp;float&nbsp;sy)&nbsp;//&nbsp;缩放</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;void&nbsp;setScale(float&nbsp;sx,&nbsp;float&nbsp;sy,&nbsp;float&nbsp;px,&nbsp;float&nbsp;py)&nbsp;//
以坐标px,py进行缩放</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;void&nbsp;setTranslate(float&nbsp;dx,&nbsp;float&nbsp;dy)&nbsp;//平移</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;void&nbsp;setSkew&nbsp;
(float&nbsp;kx,&nbsp;float&nbsp;ky,&nbsp;float&nbsp;px,&nbsp;float&nbsp;py)&nbsp;//
以坐标px,py进行倾斜</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;void&nbsp;setSkew&nbsp;
(float&nbsp;kx,&nbsp;float&nbsp;ky)&nbsp;//倾斜&nbsp;复制代码&nbsp;二、
android.graphics.NinePatch</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;NinePatch是Android平台特有的一种非矢量图形自然拉伸处理方法，可以帮助常规的</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;图形在拉伸时不会缩放，</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;实例中Android开发网提示大家对于Toast的显示就是该原理，同时SDK中提供了一</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;个工具名为Draw</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;9-Patch，有关该工具的使用方法可以参考我们经发布的&nbsp;Draw</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;9-Patch使用方法介绍一文。由于该类提供了高质量支持透明的缩放方式，所以图形</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;格式为PNG，文件命名方式为.9.png</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;的后缀比如eoeandroid。</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;三、android.graphics.Paint。Paint类我们可以理解为画笔、画刷的属性定义，本类</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;常用的方法如下:</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Java代码：&nbsp;void&nbsp;reset()&nbsp;//重置</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;void&nbsp;setARGB(int&nbsp;a,&nbsp;int&nbsp;r,&nbsp;int&nbsp;g,&nbsp;int&nbsp;b)&nbsp;
或&nbsp;void&nbsp;setColor(int&nbsp;color)</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;//均为设置Paint对象的颜色</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;void&nbsp;setAntiAlias(boolean&nbsp;aa)</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;//是否抗锯齿，需要配合void&nbsp;setFlags&nbsp;(Paint.ANTI_ALIAS_FLAG)&nbsp;来帮助消除锯齿</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;使其边缘更平滑。</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Shader&nbsp;setShader(Shader&nbsp;shader)</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;//设置阴影，Shader类是一个矩阵对象，如果为NULL将清除阴影。</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;void&nbsp;setStyle(Paint.Style&nbsp;style)&nbsp;//设置样式，一般为&nbsp;FILL&nbsp;填充，或者STROKE凹陷</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;效果。</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;void&nbsp;setTextSize(float&nbsp;textSize)&nbsp;//设置字体大小</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;void&nbsp;setTextAlign(Paint.Align&nbsp;align)&nbsp;//文本对齐方式</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Typeface&nbsp;setTypeface(Typeface&nbsp;typeface)</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;//设置字体，通过Typeface可以加载Android内部的字体，一般为宋体对于中文，</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;部分ROM可以自己添加比如雅黑等等</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;void&nbsp;setUnderlineText(boolean&nbsp;underlineText)</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;//是否设置下划线，需要撇和void&nbsp;setFlags&nbsp;(Paint.UNDERLINE_TEXT_FLAG)&nbsp;方法。&nbsp;复制代码</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;四、android.graphics.Rect</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Rect我们可以理解为矩形区域，类似的还有Point一个点，Rect类除了表示一个矩</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;形区域位置描述外，</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;eoeandroid提示主要可以帮助我们计算图形之间是否碰撞</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;(包含)关系，对于Android游戏开发比较有用，其主要的成员contains包含了三种</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;重载方法，来判断包含关系.</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Java代码：&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;boolean&nbsp;contains(int&nbsp;left,&nbsp;int&nbsp;top,&nbsp;int&nbsp;right,&nbsp;int&nbsp;bottom)</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;boolean&nbsp;contains(int&nbsp;x,&nbsp;int&nbsp;y)</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;boolean&nbsp;contains(Rect&nbsp;r)&nbsp;复制代码&nbsp;五、android.graphics.Region</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Region在Android平台中表示一个区域和Rect不同的是</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;，它表示的是一个不规则的样子，可以是椭圆、多边形等等，而Rect仅仅是矩形。</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;同样Region的boolean&nbsp;contains(int&nbsp;x,</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;int&nbsp;y)&nbsp;成员可以判断一个点是否在该区域内。</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;六、android.graphics.Typeface</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Typeface类是帮助描述一个字体对象，在TextView中通过使用setTypeface方法来</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;制定一个输出文本的字体</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;，其直接构造调用成员create方法可以直接指定一个字体名称和样式，比如</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Java代码：&nbsp;static&nbsp;Typeface&nbsp;create(Typeface&nbsp;family,&nbsp;int&nbsp;style)</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;static&nbsp;Typeface&nbsp;create(String&nbsp;familyName,&nbsp;int&nbsp;style)&nbsp;复制代码</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;同时使用isBold和isItalic方法可以判断出是否包含粗体或斜体的字型。</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Java代码：&nbsp;final&nbsp;boolean&nbsp;isBold()</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;final&nbsp;boolean&nbsp;isItalic()&nbsp;复制代码&nbsp;该类的创建方法还有从apk的资源或从一个具体的</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;文件路径，其具体方法为</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Java
代
码：&nbsp;static&nbsp;Typeface&nbsp;createFromAsset(AssetManager&nbsp;mgr,&nbsp;String&nbsp;path)</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;static&nbsp;Typeface&nbsp;createFromFile(File&nbsp;path)</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;static&nbsp;Typeface&nbsp;createFromFile(String&nbsp;path)&nbsp;复制代码</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">private</span><span>&nbsp;Paint&nbsp;paint;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;SimpleDraw(Context&nbsp;context)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">super</span><span>(context);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Paint();&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">new</span><span>&nbsp;Thread(</span><span class="keyword">this</span><span>).start();&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="annotation">@Override</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">protected</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;onDraw(Canvas&nbsp;canvas)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">super</span><span>.onDraw(canvas);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawColor(Color.GRAY);<span class="comment">//</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setAntiAlias(<span class="keyword">true</span><span>);</span><span class="comment">//</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.save();<span class="comment">//</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.clipRect(<span class="number">10</span><span>,&nbsp;</span><span class="number">10</span><span>,&nbsp;</span><span class="number">110</span><span>,&nbsp;</span><span class="number">110</span><span>);</span><span class="comment">//</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawColor(Color.WHITE);<span class="comment">//</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;canvas.rotate(m);//以屏幕左上角为坐标原点旋转</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m&nbsp;+=&nbsp;<span class="number">45</span><span>.0f;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">if</span><span>&nbsp;(m&nbsp;==&nbsp;</span><span class="number">360</span><span>.0f)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m&nbsp;=&nbsp;<span class="number">0</span><span>.0f;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.rotate(m,&nbsp;<span class="number">60</span><span>,&nbsp;</span><span class="number">60</span><span>);</span><span class="comment">//&nbsp;以(60,60)为原点旋转</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setColor(Color.GREEN);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawRect(<span class="keyword">new</span><span>&nbsp;Rect(</span><span class="number">50</span><span>,&nbsp;</span><span class="number">50</span><span>,&nbsp;</span><span class="number">70</span><span>,&nbsp;</span><span class="number">70</span><span>),&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.restore();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.save();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.translate(<span class="number">140</span><span>,&nbsp;</span><span class="number">10</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.clipRect(<span class="number">0</span><span>,&nbsp;</span><span class="number">0</span><span>,&nbsp;</span><span class="number">100</span><span>,&nbsp;</span><span class="number">100</span><span>);</span><span class="comment">//&nbsp;一定要先剪辑出矩形区域再设画布背景，</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//否则会覆盖整张画布</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawColor(Color.BLACK);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setColor(Color.BLUE);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawRect(<span class="keyword">new</span><span>&nbsp;Rect(</span><span class="number">10</span><span>,&nbsp;</span><span class="number">10</span><span>,&nbsp;</span><span class="number">50</span><span>,&nbsp;</span><span class="number">50</span><span>),&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.restore();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.save();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.translate(<span class="number">120</span><span>,&nbsp;</span><span class="number">120</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.clipRect(<span class="keyword">new</span><span>&nbsp;Rect(</span><span class="number">0</span><span>,&nbsp;</span><span class="number">0</span><span>,&nbsp;</span><span class="number">100</span><span>,&nbsp;</span><span class="number">100</span><span>));&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawColor(Color.GREEN);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;paint.setColor(Color.BLUE);</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setStrokeWidth(<span class="number">4</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setColor(Color.BLACK);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawLine(<span class="number">0</span><span>,&nbsp;</span><span class="number">60</span><span>,&nbsp;</span><span class="number">100</span><span>,&nbsp;</span><span class="number">60</span><span>,&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setARGB(<span class="number">255</span><span>,&nbsp;</span><span class="number">51</span><span>,&nbsp;</span><span class="number">51</span><span>,&nbsp;</span><span class="number">51</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setTextSize(<span class="number">20</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setFlags(Paint.ANTI_ALIAS_FLAG);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setUnderlineText(<span class="keyword">true</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;paint.setFlags(Paint.UNDERLINE_TEXT_FLAG);</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawText(<span class="string">"陈建立"</span><span>,&nbsp;</span><span class="number">25</span><span>,&nbsp;</span><span class="number">80</span><span>,&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setColor(Color.WHITE);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawRect(<span class="keyword">new</span><span>&nbsp;Rect(</span><span class="number">10</span><span>,&nbsp;</span><span class="number">10</span><span>,&nbsp;</span><span class="number">50</span><span>,&nbsp;</span><span class="number">50</span><span>),&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.restore();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">float</span><span>&nbsp;m&nbsp;=&nbsp;</span><span class="number">0</span><span>.0f;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;run()&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">while</span><span>&nbsp;(!Thread.currentThread().isInterrupted())&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">try</span><span>&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread.sleep(<span class="number">500</span><span>);</span><span class="comment">//&nbsp;每半秒执行一次</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<span class="keyword">catch</span><span>&nbsp;(InterruptedException&nbsp;e)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread.currentThread().interrupt();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;postInvalidate();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>}&nbsp;&nbsp;</span></li></ol></div><pre title="Android 图形用户界面 之 绘图（一）" pre_index="5" source_url="http://emmet1988.iteye.com/blog/1058517" codeable_type="Blog" codeable_id="1058517" style="display: none;" class="java" name="code">package com.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

public class SimpleDraw extends View implements Runnable {

	/*
	 * 我们继续来介绍Android平台底层绘图类的相关内容，在Android UI开发专题(一)
	 * 之界面设计中我们介绍了有关Android平台资源使用以及Bitmap相关类的操作
	 * ，接下来将会以实例的方式给大家演示各种类的用处以及注意点。今天我们继续
	 * 了解android.graphics包中比较重要的绘图类。
	 * 
	 * 一、 android.graphics.Matrix有关图形的变换、缩放等相关操作常用的方法有:
	 * Java代码： void reset() // 重置一个matrix对象。
	 * void set(Matrix src) //复制一个源矩阵，和本类的构造方法 Matrix(Matrix src) 一样
	 * boolean isIdentity() //返回这个矩阵是否定义(已经有意义)
	 * void setRotate(float degrees) //指定一个角度以0,0为坐标进行旋转
	 * void setRotate(float degrees, float px, float py) //指定一个角度以px,py为坐标进行旋转
	 *  void setScale(float sx, float sy) // 缩放
	 * void setScale(float sx, float sy, float px, float py) //以坐标px,py进行缩放
	 * void setTranslate(float dx, float dy) //平移
	 * void setSkew (float kx, float ky, float px, float py) //以坐标px,py进行倾斜
	 * void setSkew (float kx, float ky) //倾斜 复制代码 二、android.graphics.NinePatch
	 * 
	 * NinePatch是Android平台特有的一种非矢量图形自然拉伸处理方法，可以帮助常规的
	 * 图形在拉伸时不会缩放，
	 * 实例中Android开发网提示大家对于Toast的显示就是该原理，同时SDK中提供了一
	 * 个工具名为Draw
	 * 9-Patch，有关该工具的使用方法可以参考我们经发布的 Draw
	 * 9-Patch使用方法介绍一文。由于该类提供了高质量支持透明的缩放方式，所以图形
	 * 格式为PNG，文件命名方式为.9.png
	 * 的后缀比如eoeandroid。
	 * 
	 * 三、android.graphics.Paint。Paint类我们可以理解为画笔、画刷的属性定义，本类
	 * 常用的方法如下:
	 * 
	 * Java代码： void reset() //重置
	 * void setARGB(int a, int r, int g, int b) 或 void setColor(int color)
	 * //均为设置Paint对象的颜色
	 * void setAntiAlias(boolean aa)
	 * 
	 * //是否抗锯齿，需要配合void setFlags (Paint.ANTI_ALIAS_FLAG) 来帮助消除锯齿
	 * 使其边缘更平滑。
	 * Shader setShader(Shader shader)
	 * 
	 * //设置阴影，Shader类是一个矩阵对象，如果为NULL将清除阴影。
	 * void setStyle(Paint.Style style) //设置样式，一般为 FILL 填充，或者STROKE凹陷
	 * 效果。
	 * void setTextSize(float textSize) //设置字体大小
	 * void setTextAlign(Paint.Align align) //文本对齐方式
	 * Typeface setTypeface(Typeface typeface)
	 * //设置字体，通过Typeface可以加载Android内部的字体，一般为宋体对于中文，
	 * 部分ROM可以自己添加比如雅黑等等
	 * void setUnderlineText(boolean underlineText)
	 * 
	 * //是否设置下划线，需要撇和void setFlags (Paint.UNDERLINE_TEXT_FLAG) 方法。 复制代码
	 * 四、android.graphics.Rect
	 * 
	 * Rect我们可以理解为矩形区域，类似的还有Point一个点，Rect类除了表示一个矩
	 * 形区域位置描述外，
	 * eoeandroid提示主要可以帮助我们计算图形之间是否碰撞
	 * (包含)关系，对于Android游戏开发比较有用，其主要的成员contains包含了三种
	 * 重载方法，来判断包含关系.
	 * 
	 * Java代码： 
	 * boolean contains(int left, int top, int right, int bottom)
	 * boolean contains(int x, int y)
	 * 
	 * boolean contains(Rect r) 复制代码 五、android.graphics.Region
	 * Region在Android平台中表示一个区域和Rect不同的是
	 * ，它表示的是一个不规则的样子，可以是椭圆、多边形等等，而Rect仅仅是矩形。
	 * 同样Region的boolean contains(int x,
	 * int y) 成员可以判断一个点是否在该区域内。
	 * 
	 * 六、android.graphics.Typeface
	 * Typeface类是帮助描述一个字体对象，在TextView中通过使用setTypeface方法来
	 * 制定一个输出文本的字体
	 * ，其直接构造调用成员create方法可以直接指定一个字体名称和样式，比如
	 * Java代码： static Typeface create(Typeface family, int style)
	 * 
	 * static Typeface create(String familyName, int style) 复制代码
	 * 同时使用isBold和isItalic方法可以判断出是否包含粗体或斜体的字型。
	 * 
	 * Java代码： final boolean isBold()
	 * final boolean isItalic() 复制代码 该类的创建方法还有从apk的资源或从一个具体的
	 * 文件路径，其具体方法为
	 * Java代码： static Typeface createFromAsset(AssetManager mgr, String path)
	 * static Typeface createFromFile(File path)
	 * static Typeface createFromFile(String path) 复制代码
	 */

	private Paint paint;

	public SimpleDraw(Context context) {
		super(context);
		paint = new Paint();
		new Thread(this).start();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		canvas.drawColor(Color.GRAY);//
		paint.setAntiAlias(true);//

		canvas.save();//
		canvas.clipRect(10, 10, 110, 110);//
		canvas.drawColor(Color.WHITE);//
		// canvas.rotate(m);//以屏幕左上角为坐标原点旋转
		m += 45.0f;
		if (m == 360.0f) {
			m = 0.0f;
		}
		canvas.rotate(m, 60, 60);// 以(60,60)为原点旋转
		paint.setColor(Color.GREEN);
		canvas.drawRect(new Rect(50, 50, 70, 70), paint);
		canvas.restore();

		canvas.save();
		canvas.translate(140, 10);
		canvas.clipRect(0, 0, 100, 100);// 一定要先剪辑出矩形区域再设画布背景，
		//否则会覆盖整张画布
		canvas.drawColor(Color.BLACK);
		paint.setColor(Color.BLUE);
		canvas.drawRect(new Rect(10, 10, 50, 50), paint);
		canvas.restore();
		//
		canvas.save();
		canvas.translate(120, 120);
		canvas.clipRect(new Rect(0, 0, 100, 100));
		canvas.drawColor(Color.GREEN);
		// paint.setColor(Color.BLUE);
		paint.setStrokeWidth(4);
		paint.setColor(Color.BLACK);
		canvas.drawLine(0, 60, 100, 60, paint);
		paint.setARGB(255, 51, 51, 51);
		paint.setTextSize(20);
		paint.setFlags(Paint.ANTI_ALIAS_FLAG);
		paint.setUnderlineText(true);
		// paint.setFlags(Paint.UNDERLINE_TEXT_FLAG);
		canvas.drawText("陈建立", 25, 80, paint);
		paint.setColor(Color.WHITE);
		canvas.drawRect(new Rect(10, 10, 50, 50), paint);
		canvas.restore();
	}

	float m = 0.0f;

	public void run() {
		while (!Thread.currentThread().isInterrupted()) {
			try {
				Thread.sleep(500);// 每半秒执行一次
			} catch (InterruptedException e) {
				Thread.currentThread().interrupt();
			}
			postInvalidate();
		}
	}
}
</pre>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>TextDraw.java</p>
<p>&nbsp;</p>
<div id="" class="dp-highlighter"><div class="bar"><div class="tools">Java代码 <embed wmode="transparent" src="1058517_files/clipboard_new.swf" flashvars="clipboard=package%20com.view%3B%0A%0Aimport%20android.content.Context%3B%0Aimport%20android.graphics.Canvas%3B%0Aimport%20android.graphics.Color%3B%0Aimport%20android.graphics.Paint%3B%0Aimport%20android.graphics.Paint.FontMetrics%3B%0Aimport%20android.util.Log%3B%0Aimport%20android.view.View%3B%0A%0Apublic%20class%20TextDraw%20extends%20View%20%7B%0A%0A%09public%20TextDraw(Context%20context)%20%7B%0A%09%09super(context)%3B%0A%09%7D%0A%0A%09%40Override%0A%09protected%20void%20onDraw(Canvas%20canvas)%20%7B%0A%09%09super.onDraw(canvas)%3B%0A%09%09Paint%20textPaint%20%3D%20new%20Paint(%20Paint.ANTI_ALIAS_FLAG)%3B%0A%09%09textPaint.setTextSize(%2035)%3B%0A%09%09textPaint.setColor(%20Color.WHITE)%3B%0A%0A%09%09%2F%2F%20FontMetrics%0A%09%09FontMetrics%20fontMetrics%20%3D%20textPaint.getFontMetrics()%3B%0A%0A%09%09String%20text%20%3D%20%22abcdefghijklm%22%3B%0A%0A%09%09%2F%2F%20%0A%09%09float%20baseX%20%3D%200%3B%0A%09%09float%20baseY%20%3D%20100%3B%0A%09%09Log.d(%22textDraw%22%2C%20%22top%20%3D%20%22%2BfontMetrics.top%2B%0A%09%09%09%09%09%09%09%09%09%09%22ascent%20%3D%20%22%2BfontMetrics.ascent%2B%0A%09%09%09%09%09%09%09%09%09%09%22descent%20%3D%20%22%2BfontMetrics.descent%2B%0A%09%09%09%09%09%09%09%09%09%09%22bottom%20%3D%20%22%2BfontMetrics.bottom%2B%22%5Cn%22)%3B%0A%09%09float%20topY%20%3D%20baseY%20%2B%20fontMetrics.top%3B%0A%09%09float%20ascentY%20%3D%20baseY%20%2B%20fontMetrics.ascent%3B%0A%09%09float%20descentY%20%3D%20baseY%20%2B%20fontMetrics.descent%3B%0A%09%09float%20bottomY%20%3D%20baseY%20%2B%20fontMetrics.bottom%3B%0A%09%09Log.d(%22textDraw%22%2C%20%22topY%20%3D%20%22%2BtopY%2B%0A%09%09%09%09%09%09%09%09%09%09%22ascentY%20%3D%20%22%2BascentY%2B%0A%09%09%09%09%09%09%09%09%09%09%22descentY%20%3D%20%22%2BdescentY%2B%0A%09%09%09%09%09%09%09%09%09%09%22bottomY%20%3D%20%22%2BbottomY)%3B%0A%09%09%2F%2F%20%0A%09%09canvas.drawText(%20text%2C%20baseX%2C%20baseY%2C%20textPaint)%3B%0A%0A%09%09%2F%2F%20BaseLine%0A%09%09Paint%20baseLinePaint%20%3D%20new%20Paint(%20Paint.ANTI_ALIAS_FLAG)%3B%0A%09%09baseLinePaint.setColor(%20Color.RED)%3B%0A%09%09canvas.drawLine(0%2C%20baseY%2C%20getWidth()%2C%20baseY%2C%20baseLinePaint)%3B%0A%0A%09%09%2F%2F%20Base%0A%09%09canvas.drawCircle(%20baseX%2C%20baseY%2C%205%2C%20baseLinePaint)%3B%0A%0A%09%09%2F%2F%20TopLine%0A%09%09Paint%20topLinePaint%20%3D%20new%20Paint(%20Paint.ANTI_ALIAS_FLAG)%3B%0A%09%09topLinePaint.setColor(%20Color.LTGRAY)%3B%0A%09%09canvas.drawLine(0%2C%20topY%2C%20getWidth()%2C%20topY%2C%20topLinePaint)%3B%0A%0A%09%09%2F%2F%20AscentLine%0A%09%09Paint%20ascentLinePaint%20%3D%20new%20Paint(%20Paint.ANTI_ALIAS_FLAG)%3B%0A%09%09ascentLinePaint.setColor(%20Color.GREEN)%3B%0A%09%09canvas.drawLine(0%2C%20ascentY%2C%20getWidth()%2C%20ascentY%2C%20ascentLinePaint)%3B%0A%0A%09%09%2F%2F%20DescentLine%0A%09%09Paint%20descentLinePaint%20%3D%20new%20Paint(%20Paint.ANTI_ALIAS_FLAG)%3B%0A%09%09descentLinePaint.setColor(%20Color.YELLOW)%3B%0A%09%09canvas.drawLine(0%2C%20descentY%2C%20getWidth()%2C%20descentY%2C%20descentLinePaint)%3B%0A%0A%09%09%2F%2F%20ButtomLine%0A%09%09Paint%20bottomLinePaint%20%3D%20new%20Paint(%20Paint.ANTI_ALIAS_FLAG)%3B%0A%09%09bottomLinePaint.setColor(%20Color.MAGENTA)%3B%0A%09%09canvas.drawLine(0%2C%20bottomY%2C%20getWidth()%2C%20bottomY%2C%20bottomLinePaint)%3B%0A%09%7D%0A%7D%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="15" width="14">&nbsp;<a href="javascript:void()" title="收藏这段代码" onclick="code_favorites_do_favorite(this);return false;"><img class="star" src="1058517_files/icon_star.png" alt="收藏代码"><img class="spinner" src="1058517_files/spinner.gif" style="display:none"></a></div></div><ol class="dp-j" start="1"><li><span><span class="keyword">package</span><span>&nbsp;com.view;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span><span class="keyword">import</span><span>&nbsp;android.content.Context;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.Canvas;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.Color;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.Paint;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.Paint.FontMetrics;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.util.Log;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.view.View;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span><span class="keyword">public</span><span>&nbsp;</span><span class="keyword">class</span><span>&nbsp;TextDraw&nbsp;</span><span class="keyword">extends</span><span>&nbsp;View&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;TextDraw(Context&nbsp;context)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">super</span><span>(context);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="annotation">@Override</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">protected</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;onDraw(Canvas&nbsp;canvas)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">super</span><span>.onDraw(canvas);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Paint&nbsp;textPaint&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Paint(&nbsp;Paint.ANTI_ALIAS_FLAG);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;textPaint.setTextSize(&nbsp;<span class="number">35</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;textPaint.setColor(&nbsp;Color.WHITE);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;FontMetrics</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FontMetrics&nbsp;fontMetrics&nbsp;=&nbsp;textPaint.getFontMetrics();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;text&nbsp;=&nbsp;<span class="string">"abcdefghijklm"</span><span>;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">float</span><span>&nbsp;baseX&nbsp;=&nbsp;</span><span class="number">0</span><span>;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">float</span><span>&nbsp;baseY&nbsp;=&nbsp;</span><span class="number">100</span><span>;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.d(<span class="string">"textDraw"</span><span>,&nbsp;</span><span class="string">"top&nbsp;=&nbsp;"</span><span>+fontMetrics.top+&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="string">"ascent&nbsp;=&nbsp;"</span><span>+fontMetrics.ascent+&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="string">"descent&nbsp;=&nbsp;"</span><span>+fontMetrics.descent+&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="string">"bottom&nbsp;=&nbsp;"</span><span>+fontMetrics.bottom+</span><span class="string">"\n"</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">float</span><span>&nbsp;topY&nbsp;=&nbsp;baseY&nbsp;+&nbsp;fontMetrics.top;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">float</span><span>&nbsp;ascentY&nbsp;=&nbsp;baseY&nbsp;+&nbsp;fontMetrics.ascent;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">float</span><span>&nbsp;descentY&nbsp;=&nbsp;baseY&nbsp;+&nbsp;fontMetrics.descent;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">float</span><span>&nbsp;bottomY&nbsp;=&nbsp;baseY&nbsp;+&nbsp;fontMetrics.bottom;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.d(<span class="string">"textDraw"</span><span>,&nbsp;</span><span class="string">"topY&nbsp;=&nbsp;"</span><span>+topY+&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="string">"ascentY&nbsp;=&nbsp;"</span><span>+ascentY+&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="string">"descentY&nbsp;=&nbsp;"</span><span>+descentY+&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="string">"bottomY&nbsp;=&nbsp;"</span><span>+bottomY);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawText(&nbsp;text,&nbsp;baseX,&nbsp;baseY,&nbsp;textPaint);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;BaseLine</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Paint&nbsp;baseLinePaint&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Paint(&nbsp;Paint.ANTI_ALIAS_FLAG);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;baseLinePaint.setColor(&nbsp;Color.RED);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawLine(<span class="number">0</span><span>,&nbsp;baseY,&nbsp;getWidth(),&nbsp;baseY,&nbsp;baseLinePaint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;Base</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawCircle(&nbsp;baseX,&nbsp;baseY,&nbsp;<span class="number">5</span><span>,&nbsp;baseLinePaint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;TopLine</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Paint&nbsp;topLinePaint&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Paint(&nbsp;Paint.ANTI_ALIAS_FLAG);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;topLinePaint.setColor(&nbsp;Color.LTGRAY);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawLine(<span class="number">0</span><span>,&nbsp;topY,&nbsp;getWidth(),&nbsp;topY,&nbsp;topLinePaint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;AscentLine</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Paint&nbsp;ascentLinePaint&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Paint(&nbsp;Paint.ANTI_ALIAS_FLAG);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ascentLinePaint.setColor(&nbsp;Color.GREEN);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawLine(<span class="number">0</span><span>,&nbsp;ascentY,&nbsp;getWidth(),&nbsp;ascentY,&nbsp;ascentLinePaint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;DescentLine</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Paint&nbsp;descentLinePaint&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Paint(&nbsp;Paint.ANTI_ALIAS_FLAG);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;descentLinePaint.setColor(&nbsp;Color.YELLOW);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawLine(<span class="number">0</span><span>,&nbsp;descentY,&nbsp;getWidth(),&nbsp;descentY,&nbsp;descentLinePaint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;ButtomLine</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Paint&nbsp;bottomLinePaint&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Paint(&nbsp;Paint.ANTI_ALIAS_FLAG);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bottomLinePaint.setColor(&nbsp;Color.MAGENTA);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawLine(<span class="number">0</span><span>,&nbsp;bottomY,&nbsp;getWidth(),&nbsp;bottomY,&nbsp;bottomLinePaint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>}&nbsp;&nbsp;</span></li></ol></div><pre title="Android 图形用户界面 之 绘图（一）" pre_index="6" source_url="http://emmet1988.iteye.com/blog/1058517" codeable_type="Blog" codeable_id="1058517" style="display: none;" class="java" name="code">package com.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.util.Log;
import android.view.View;

public class TextDraw extends View {

	public TextDraw(Context context) {
		super(context);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Paint textPaint = new Paint( Paint.ANTI_ALIAS_FLAG);
		textPaint.setTextSize( 35);
		textPaint.setColor( Color.WHITE);

		// FontMetrics
		FontMetrics fontMetrics = textPaint.getFontMetrics();

		String text = "abcdefghijklm";

		// 
		float baseX = 0;
		float baseY = 100;
		Log.d("textDraw", "top = "+fontMetrics.top+
										"ascent = "+fontMetrics.ascent+
										"descent = "+fontMetrics.descent+
										"bottom = "+fontMetrics.bottom+"\n");
		float topY = baseY + fontMetrics.top;
		float ascentY = baseY + fontMetrics.ascent;
		float descentY = baseY + fontMetrics.descent;
		float bottomY = baseY + fontMetrics.bottom;
		Log.d("textDraw", "topY = "+topY+
										"ascentY = "+ascentY+
										"descentY = "+descentY+
										"bottomY = "+bottomY);
		// 
		canvas.drawText( text, baseX, baseY, textPaint);

		// BaseLine
		Paint baseLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);
		baseLinePaint.setColor( Color.RED);
		canvas.drawLine(0, baseY, getWidth(), baseY, baseLinePaint);

		// Base
		canvas.drawCircle( baseX, baseY, 5, baseLinePaint);

		// TopLine
		Paint topLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);
		topLinePaint.setColor( Color.LTGRAY);
		canvas.drawLine(0, topY, getWidth(), topY, topLinePaint);

		// AscentLine
		Paint ascentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);
		ascentLinePaint.setColor( Color.GREEN);
		canvas.drawLine(0, ascentY, getWidth(), ascentY, ascentLinePaint);

		// DescentLine
		Paint descentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);
		descentLinePaint.setColor( Color.YELLOW);
		canvas.drawLine(0, descentY, getWidth(), descentY, descentLinePaint);

		// ButtomLine
		Paint bottomLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG);
		bottomLinePaint.setColor( Color.MAGENTA);
		canvas.drawLine(0, bottomY, getWidth(), bottomY, bottomLinePaint);
	}
}
</pre>
<p>&nbsp;</p>
<p>RoundAndReflectionDraw.java</p>
<p>&nbsp;</p>
<div id="" class="dp-highlighter"><div class="bar"><div class="tools">Java代码 <embed wmode="transparent" src="1058517_files/clipboard_new.swf" flashvars="clipboard=class%20RoundImageView%20extends%20View%20%7B%0A%0A%09%09private%20Bitmap%20bitmap%3B%0A%09%09int%20bitmapWidth%3B%0A%09%09int%20bitmapHeight%3B%0A%0A%09%09public%20RoundImageView(Context%20context)%20%7B%0A%09%09%09super(context)%3B%0A%09%09%09bitmap%20%3D%20BitmapFactory.decodeResource(getResources()%2CR.drawable.rotate_surfaceview)%3B%0A%09%09%09bitmapWidth%20%3D%20bitmap.getWidth()%3B%0A%09%09%09bitmapHeight%20%3D%20bitmap.getHeight()%3B%0A%09%09%7D%0A%0A%09%09%40Override%0A%09%09protected%20void%20onDraw(Canvas%20canvas)%20%7B%0A%09%09%09super.onDraw(canvas)%3B%0A%09%09%09%2F%2F%20%E7%AC%AC%E4%B8%80%E7%A7%8D%E6%96%B9%E6%B3%95%EF%BC%9A%0A%09%09%09%2F*Bitmap%20roundBitmap%20%3D%20Bitmap.createBitmap(bitmapWidth%2C%20bitmapHeight%2CBitmap.Config.ARGB_8888)%3B%0A%09%09%09canvas%20%3D%20new%20Canvas(roundBitmap)%3B%0A%09%09%09Paint%20paint%20%3D%20new%20Paint(Paint.ANTI_ALIAS_FLAG)%3B%0A%09%09%09paint.setColor(Color.BLUE)%3B%0A%09%09%09canvas.drawRoundRect(new%20RectF(0%2C%200%2C%20bitmapWidth%2C%20bitmapHeight)%2C20.0f%2C%2020.0f%2C%20paint)%3B%0A%09%09%09paint.setXfermode(new%20PorterDuffXfermode(PorterDuff.Mode.DST_IN))%3B%0A%09%09%09canvas.drawBitmap(bitmap%2C%200%2C%200%2C%20null)%3B%0A%09%09%09canvas.drawBitmap(roundBitmap%2C%200%2C%200%2C%20paint)%3B*%2F%0A%09%09%09%2F%2F%20%E7%AC%AC%E4%BA%8C%E7%A7%8D%E6%96%B9%E6%B3%95%EF%BC%9A%0A%09%09%09Paint%20paint%20%3D%20new%20Paint(Paint.ANTI_ALIAS_FLAG)%3B%0A%09%09%09paint.setColor(0xffffffff)%3B%0A%09%09%09paint.setTextSize(15)%3B%0A%09%09%09canvas.drawText(%22%E7%94%9F%E6%88%90%E5%B8%A6%E5%9C%86%E8%A7%92%E7%9A%84%E5%9B%BE%E7%89%87%22%2C%2010%2C%2025%2C%20paint)%3B%0A%09%09%09canvas.drawBitmap(getRoundedCornerBitmap(bitmap)%2C%2010%2C%2030%2C%20paint)%3B%0A%09%09%09%0A%09%09%09canvas.drawText(%22%E7%94%9F%E6%88%90%E5%B8%A6%E5%80%92%E5%BD%B1%E7%9A%84%E5%9B%BE%E7%89%87%22%2C%20170%2C%20160%2C%20paint)%3B%0A%09%09%09canvas.drawBitmap(createReflectionImageWithOrigin(bitmap)%2C%20170%2C%20165%2C%20paint)%3B%0A%09%09%09%0A%09%09%7D%0A%09%09%0A%09%09public%20Bitmap%20getRoundedCornerBitmap(Bitmap%20bitmap)%20%7B%20%0A%09%09%09%2F%2F%20%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E6%8C%87%E5%AE%9A%E5%AE%BD%E5%BA%A6%E5%92%8C%E9%AB%98%E5%BA%A6%E7%9A%84%E7%A9%BA%E4%BD%8D%E5%9B%BE%E5%AF%B9%E8%B1%A1%0A%09%20%20%09%09Bitmap%20output%20%3D%20Bitmap.createBitmap(bitmap.getWidth()%2Cbitmap.getHeight()%2C%20Config.ARGB_8888)%3B%20%0A%09%20%09%09%2F%2F%20%E7%94%A8%E8%AF%A5%E4%BD%8D%E5%9B%BE%E5%88%9B%E5%BB%BA%E7%94%BB%E5%B8%83%0A%09%20%20%09%09Canvas%20canvas%20%3D%20new%20Canvas(output)%3B%0A%09%20%20%09%09%2F%2F%20%E7%94%BB%E7%AC%94%E5%AF%B9%E8%B1%A1%0A%09%20%20%09%09final%20Paint%20paint%20%3D%20new%20Paint()%3B%20%0A%09%20%20%09%09%2F%2F%20%E7%94%BB%E7%AC%94%E7%9A%84%E9%A2%9C%E8%89%B2%0A%09%20%20%09%09final%20int%20color%20%3D%200xff424242%3B%20%0A%09%20%20%09%09%2F%2F%20%E7%9F%A9%E5%BD%A2%E5%8C%BA%E5%9F%9F%E5%AF%B9%E8%B1%A1%0A%09%20%20%09%09final%20Rect%20rect%20%3D%20new%20Rect(0%2C%200%2C%20bitmap.getWidth()%2C%20bitmap.getHeight())%3B%20%0A%09%20%20%09%09%2F%2F%20%E6%9C%AA%E7%9F%A5%0A%09%20%20%09%09final%20RectF%20rectF%20%3D%20new%20RectF(rect)%3B%20%0A%09%20%20%09%09%2F%2F%20%E6%8B%90%E8%A7%92%E7%9A%84%E5%8D%8A%E5%BE%84%0A%09%20%20%09%09final%20float%20roundPx%20%3D%2012%3B%0A%09%20%20%09%09%2F%2F%20%E6%B6%88%E9%99%A4%E9%94%AF%E9%BD%BF%0A%09%20%20%09%09paint.setAntiAlias(true)%3B%20%0A%09%20%20%09%09%2F%2F%20%E7%94%BB%E5%B8%83%E8%83%8C%E6%99%AF%E8%89%B2%0A%09%20%20%09%09canvas.drawARGB(0%2C%200%2C%200%2C%200)%3B%0A%09%20%20%09%09%2F%2F%20%E8%AE%BE%E7%BD%AE%E7%94%BB%E7%AC%94%E9%A2%9C%E8%89%B2%0A%09%20%20%09%09paint.setColor(color)%3B%20%0A%09%20%20%09%09%2F%2F%20%E7%BB%98%E5%88%B6%E5%9C%86%E8%A7%92%E7%9F%A9%E5%BD%A2%0A%09%20%20%09%09canvas.drawRoundRect(rectF%2C%20roundPx%2C%20roundPx%2Cpaint)%3B%0A%09%20%20%09%09%2F%2F%20%E6%9C%AA%E7%9F%A5%0A%09%20%20%09%09paint.setXfermode(new%20PorterDuffXfermode(Mode.SRC_IN))%3B%0A%09%20%20%09%09%2F%2F%20%E6%8A%8A%E8%AF%A5%E5%9B%BE%E7%89%87%E7%BB%98%E5%88%B6%E5%9C%A8%E8%AF%A5%E5%9C%86%E8%A7%92%E7%9F%A9%E5%BD%A2%E5%8C%BA%E5%9F%9F%E4%B8%AD%0A%09%20%20%09%09canvas.drawBitmap(bitmap%2C%20rect%2C%20rect%2C%20paint)%3B%0A%09%20%20%09%09%2F%2F%20%E6%9C%80%E7%BB%88%E5%9C%A8%E7%94%BB%E5%B8%83%E4%B8%8A%E5%91%88%E7%8E%B0%E7%9A%84%E5%B0%B1%E6%98%AF%E8%AF%A5%E5%9C%86%E8%A7%92%E7%9F%A9%E5%BD%A2%E5%9B%BE%E7%89%87%EF%BC%8C%E7%84%B6%E5%90%8E%E6%88%91%E4%BB%AC%E8%BF%94%E5%9B%9E%E8%AF%A5Bitmap%E5%AF%B9%E8%B1%A1%0A%09%09%09return%20output%3B%20%0A%09%09%7D%0A%09%09%0A%09%09%2F%2F%E8%8E%B7%E5%BE%97%E5%B8%A6%E5%80%92%E5%BD%B1%E7%9A%84%E5%9B%BE%E7%89%87%E6%96%B9%E6%B3%95%20%0A%09%09public%20Bitmap%20createReflectionImageWithOrigin(Bitmap%20bitmap)%7B%20%0A%09%09%09%2F%2F%20%E5%9B%BE%E7%89%87%E4%B8%8E%E5%80%92%E5%BD%B1%E4%B9%8B%E9%97%B4%E7%9A%84%E8%B7%9D%E7%A6%BB%E9%97%B4%E9%9A%94%0A%09%09%09final%20int%20reflectionGap%20%3D%202%3B%0A%09%09%09%2F%2F%20%E5%8E%9F%E5%9B%BE%E7%9A%84%E5%AE%BD%E5%BA%A6%0A%09%09%09int%20width%20%3D%20bitmap.getWidth()%3B%20%0A%09%09%09%2F%2F%20%E5%8E%9F%E5%9B%BE%E7%9A%84%E9%AB%98%E5%BA%A6%0A%09%09%09int%20height%20%3D%20bitmap.getHeight()%3B%20%0A%09%09%09%2F%2F%20%E5%9B%BE%E7%89%87%E6%97%8B%E8%BD%AC%EF%BC%8C%E7%BC%A9%E6%94%BE%E7%AD%89%E6%8E%A7%E5%88%B6%E5%AF%B9%E8%B1%A1%0A%09%09%09Matrix%20matrix%20%3D%20new%20Matrix()%3B%20%0A%09%09%09%2F%2F%20%E7%BC%A9%E6%94%BE%EF%BC%88%E8%BF%99%E9%87%8Cpre%2Cset%2Cpost%E4%B8%89%E7%A7%8D%E6%95%88%E6%9E%9C%E6%98%AF%E4%B8%8D%E4%B8%80%E6%A0%B7%E7%9A%84%EF%BC%8C%E6%B3%A8%E6%84%8F%E5%8C%BA%E5%88%AB%EF%BC%89%0A%09%09%09matrix.preScale(1%2C%20-1)%3B%20%0A%09%09%09%2F**%0A%09%09%09%20%20%09set%E6%98%AF%E7%9B%B4%E6%8E%A5%E8%AE%BE%E7%BD%AEMatrix%E7%9A%84%E5%80%BC%EF%BC%8C%E6%AF%8F%E6%AC%A1set%E4%B8%80%E6%AC%A1%EF%BC%8C%E6%95%B4%E4%B8%AAMatrix%E7%9A%84%E6%95%B0%E7%BB%84%E9%83%BD%E4%BC%9A%E5%8F%98%E6%8E%89%E3%80%82%20%0A%09%09%09%09post%E6%98%AF%E5%90%8E%E4%B9%98%EF%BC%8C%E5%BD%93%E5%89%8D%E7%9A%84%E7%9F%A9%E9%98%B5%E4%B9%98%E4%BB%A5%E5%8F%82%E6%95%B0%E7%BB%99%E5%87%BA%E7%9A%84%E7%9F%A9%E9%98%B5%E3%80%82%E5%8F%AF%E4%BB%A5%E8%BF%9E%E7%BB%AD%E5%A4%9A%E6%AC%A1%E4%BD%BF%E7%94%A8post%EF%BC%8C%0A%09%09%09%09%E6%9D%A5%E5%AE%8C%E6%88%90%E6%89%80%E9%9C%80%E7%9A%84%E6%95%B4%E4%B8%AA%E5%8F%98%E6%8D%A2%E3%80%82%E4%BE%8B%E5%A6%82%EF%BC%8C%E8%A6%81%E5%B0%86%E4%B8%80%E4%B8%AA%E5%9B%BE%E7%89%87%E6%97%8B%0A%09%09%09%09%E8%BD%AC30%E5%BA%A6%EF%BC%8C%E7%84%B6%E5%90%8E%E5%B9%B3%E7%A7%BB%E5%88%B0(100%2C100)%E7%9A%84%E5%9C%B0%E6%96%B9%EF%BC%8C%E9%82%A3%E4%B9%88%E5%8F%AF%E4%BB%A5%E8%BF%99%E6%A0%B7%E5%81%9A%3A%20%0A%09%09%09%09Matrix%20m%20%3D%20new%20Matrix()%3B%0A%09%09%09%09m.postRotate(30)%3B%0A%09%09%09%09m.postTranslate(100%2C%20100)%3B%20%20%0A%09%09%09%09%E8%BF%99%E6%A0%B7%E5%B0%B1%E8%BE%BE%E5%88%B0%E4%BA%86%E6%83%B3%E8%A6%81%E7%9A%84%E6%95%88%E6%9E%9C%E3%80%82%20%0A%09%09%09%09pre%E6%98%AF%E5%89%8D%E4%B9%98%EF%BC%8C%E5%8F%82%E6%95%B0%E7%BB%99%E5%87%BA%E7%9A%84%E7%9F%A9%E9%98%B5%E4%B9%98%E4%BB%A5%E5%BD%93%E5%89%8D%E7%9A%84%E7%9F%A9%E9%98%B5%E3%80%82%E6%89%80%E4%BB%A5%E6%93%8D%E4%BD%9C%E6%98%AF%E5%9C%A8%E5%BD%93%E5%89%8D%E7%9F%A9%E9%98%B5%E7%9A%84%E6%9C%80%E5%89%8D%E9%9D%A2%E5%8F%91%E7%94%9F%E7%9A%84%E3%80%82%0A%09%09%09%09%E4%BE%8B%E5%A6%82%E4%B8%8A%E9%9D%A2%E7%9A%84%E4%BE%8B%E5%AD%90%EF%BC%8C%E5%A6%82%E6%9E%9C%E7%94%A8pre%E7%9A%84%E8%AF%9D%EF%BC%8C%E5%B0%B1%E8%A6%81%E8%BF%99%E6%A0%B7%3A%0A%09%09%09%09Matrix%20m%20%3D%20new%20Matrix()%3B%0A%09%09%09%09m.setTranslate(100%2C%20100)%3B%0A%09%09%09%09m.preRotate(30)%3B%0A%09%09%09%09%E6%97%8B%E8%BD%AC%E3%80%81%E7%BC%A9%E6%94%BE%E5%92%8C%E5%80%BE%E6%96%9C%E9%83%BD%E5%8F%AF%E4%BB%A5%E5%9B%B4%E7%BB%95%E4%B8%80%E4%B8%AA%E4%B8%AD%E5%BF%83%E7%82%B9%E6%9D%A5%E8%BF%9B%E8%A1%8C%EF%BC%8C%E5%A6%82%E6%9E%9C%E4%B8%8D%E6%8C%87%E5%AE%9A%EF%BC%8C%E9%BB%98%E8%AE%A4%E6%83%85%E5%86%B5%E4%B8%8B%EF%BC%8C%0A%09%09%09%09%E6%98%AF%E5%9B%B4%E7%BB%95(0%2C0)%E7%82%B9%E6%9D%A5%E8%BF%9B%E8%A1%8C%E3%80%82%20%0A%09%09%09%09%0A%09%09%09%09%E5%85%B3%E4%BA%8E%E7%BC%A9%E6%94%BE%EF%BC%9A%0A%09%09%09%09scale%E7%9A%84%E5%8F%82%E6%95%B0%E6%98%AF%E6%AF%94%E4%BE%8B%E3%80%82%E4%BE%8B%E5%A6%82%EF%BC%8C%E6%88%91%E4%BB%AC%E7%BC%A9%E6%94%BE%E4%B8%BA100%25%EF%BC%8C%E5%88%99%E6%9C%89%E4%B8%80%E7%82%B9%E8%A6%81%E6%B3%A8%E6%84%8F%EF%BC%8C%E5%A6%82%E6%9E%9C%E7%9B%B4%E6%8E%A5%E7%94%A8%0A%09%09%09%09100%2Fbmp.getWidth()%E7%9A%84%E8%AF%9D%EF%BC%8C%E4%BC%9A%E5%BE%97%E5%88%B00%EF%BC%8C%E5%9B%A0%E4%B8%BA%E6%98%AF%E6%95%B4%E5%9E%8B%E7%9B%B8%E9%99%A4%EF%BC%8C%E6%89%80%E4%BB%A5%E5%BF%85%E9%A1%BB%E5%85%B6%E4%B8%AD%E6%9C%89%E4%B8%80%E4%B8%AA%E6%98%AF%0A%09%09%09%09float%E5%9E%8B%E7%9A%84%EF%BC%8C%E7%9B%B4%E6%8E%A5%E7%94%A8100f%E5%B0%B1%E5%A5%BD%20%E3%80%82%0A%09%09%09%09%E5%A6%82%EF%BC%9Amatrix.setScale(100f%2Fbmp.getWidth()%2C%20100f%2Fbmp.getHeight())%3B%20%20%20%0A%09%09%09%20*%2F%0A%09%09%09%2F%2F%20%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E5%88%9D%E5%A7%8B%E7%9A%84%E5%80%92%E5%BD%B1%E4%BD%8D%E5%9B%BE%0A%09%09%09Bitmap%20reflectionImage%20%3D%20Bitmap.createBitmap(bitmap%2C%200%2C%20height%2F2%2C%20width%2C%20height%2F2%2C%20matrix%2C%20false)%3B%20%0A%09%09%09%2F%2F%20%E6%96%B0%E5%BB%BA%E4%B8%80%E4%B8%AA%E5%AE%BD%E5%BA%A6%E4%B8%BA%E5%8E%9F%E5%9B%BE%E5%AE%BD%E5%BA%A6%EF%BC%8C%E9%AB%98%E5%BA%A6%E4%B8%BA%E5%8E%9F%E5%9B%BE%E9%AB%98%E5%BA%A6%E7%9A%843%2F2%E7%9A%84%E4%BD%8D%E5%9B%BE%EF%BC%8C%E7%94%A8%E4%BA%8E%E7%BB%98%E5%88%B6%E6%96%B0%E7%9A%84%E4%BD%8D%E5%9B%BE%EF%BC%8C%E5%8D%B3%E6%95%B4%E4%BD%93%E7%9A%84%E6%95%88%E6%9E%9C%E5%9B%BE%E4%BD%8D%E5%9B%BE%E5%AF%B9%E8%B1%A1%0A%09%09%09Bitmap%20bitmapWithReflection%20%3D%20Bitmap.createBitmap(width%2C%20(height%20%2B%20height%2F2)%2C%20Config.ARGB_8888)%3B%20%0A%09%09%09%2F%2F%20%E7%94%B1%E8%AF%A5%E4%BD%8D%E5%9B%BE%E5%AF%B9%E8%B1%A1%E5%88%9B%E5%BB%BA%E5%88%9D%E5%A7%8B%E7%94%BB%E5%B8%83(%E8%A7%84%E5%AE%9A%E4%BA%86%E7%94%BB%E5%B8%83%E7%9A%84%E5%AE%BD%E9%AB%98)%0A%09%09%09Canvas%20canvas%20%3D%20new%20Canvas(bitmapWithReflection)%3B%20%0A%09%09%09%2F%2F%20%E5%9C%A8%E8%AF%A5%E7%94%BB%E5%B8%83%E4%B8%8A%E7%BB%98%E5%88%B6%E5%8E%9F%E5%9B%BE%0A%09%09%09canvas.drawBitmap(bitmap%2C%200%2C%200%2C%20null)%3B%20%0A%09%09%09%2F%2F%20%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E7%94%BB%E7%AC%94%0A%09%09%09Paint%20deafalutPaint%20%3D%20new%20Paint()%3B%20%0A%09%09%09%2F%2F%20%E7%BB%98%E5%88%B6%E4%B8%80%E4%B8%AA%E7%9F%A9%E5%BD%A2%E5%8C%BA%E5%9F%9F%EF%BC%8C%E8%AF%A5%E7%9F%A9%E5%BD%A2%E5%8C%BA%E5%9F%9F%E4%BE%BF%E6%98%AF%E5%8E%9F%E5%9B%BE%E5%92%8C%E5%80%92%E5%BD%B1%E5%9B%BE%E4%B9%8B%E9%97%B4%E7%9A%84%E9%97%B4%E9%9A%94%E5%9B%BE%0A%09%09%09canvas.drawRect(0%2C%20height%2Cwidth%2Cheight%20%2B%20reflectionGap%2CdeafalutPaint)%3B%20%0A%09%09%09%2F%2F%20%E7%BB%98%E5%88%B6%E8%AF%A5%E5%80%92%E5%BD%B1%E5%9B%BE%E4%BA%8E%E9%97%B4%E9%9A%94%E5%9B%BE%E7%9A%84%E4%B8%8B%E6%96%B9%0A%09%09%09canvas.drawBitmap(reflectionImage%2C%200%2C%20height%20%2B%20reflectionGap%2C%20null)%3B%20%0A%09%09%09%2F%2F%20%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E7%94%BB%E7%AC%94%0A%09%09%09Paint%20paint%20%3D%20new%20Paint()%3B%0A%09%09%09%2F%2F%20%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E7%BA%BF%E6%80%A7%E6%B8%90%E5%8F%98%E5%AF%B9%E8%B1%A1%0A%09%09%09LinearGradient%20shader%20%3D%20new%20LinearGradient(%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09%090%2C%20bitmap.getHeight()%2C%20%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09%090%2C%20bitmapWithReflection.getHeight()%20%2B%20reflectionGap%2C%20%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09%090x70ffffff%2C%200x00ffffff%2C%20%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09TileMode.CLAMP%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%20%20)%3B%20%0A%09%09%09%2F%2F%20%E6%8A%8A%E6%B8%90%E5%8F%98%E6%95%88%E6%9E%9C%E5%BA%94%E7%94%A8%E5%9C%A8%E7%94%BB%E7%AC%94%E4%B8%8A%0A%09%09%09paint.setShader(shader)%3B%20%0A%09%09%09%2F%2F%20Set%20the%20Transfer%20mode%20to%20be%20porter%20duff%20and%20destination%20in%20%0A%09%09%09%2F%2F%20%E6%9C%AA%E7%9F%A5%0A%09%09%09paint.setXfermode(new%20PorterDuffXfermode(Mode.DST_IN))%3B%20%0A%09%09%09%2F%2F%20Draw%20a%20rectangle%20using%20the%20paint%20with%20our%20linear%20gradient%20%0A%09%09%09%2F%2F%20%E7%BB%98%E5%88%B6%E5%87%BA%E8%AF%A5%E6%B8%90%E5%8F%98%E6%95%88%E6%9E%9C%EF%BC%8C%E4%B9%9F%E5%B0%B1%E6%98%AF%E6%9C%80%E7%BB%88%E7%9A%84%E5%80%92%E5%BD%B1%E6%95%88%E6%9E%9C%E5%9B%BE%0A%09%09%09canvas.drawRect(0%2C%20height%2C%20width%2C%20bitmapWithReflection.getHeight()%20%2B%20reflectionGap%2C%20paint)%3B%20%0A%09%09%09%2F%2F%20%E8%BF%94%E5%9B%9E%0A%09%09%09return%20bitmapWithReflection%3B%20%0A%09%09%7D%20%0A%0A%09%7D%0A%0A%09%2F**%0A%09%20*%20%E7%BB%98%E5%88%B6%E5%9C%86%E8%A7%92%E8%83%8C%E6%99%AF%E4%BB%A5%E5%8F%8A%E5%9B%BE%E7%89%87%E5%9C%86%E8%A7%92%E7%9A%84%E5%A4%84%E7%90%86%0A%09%20%20%20.%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E5%AE%9E%E7%8E%B0%20%0A%09%20%20%20%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%3E%20%0A%09%20%20%20%3Clayer-list%0A%09%20*%20%09%09xmlns%3Aandroid%3D%22http%3A%2F%2Fschemas.android.com%2Fapk%2Fres%2Fandroid%22%3E%20%3Citem%0A%09%20*%20%09%09android%3Adrawable%3D%22%40drawable%2Ficon_home_button_img%22%2F%3E%20%3Citem%0A%09%20*%20%09%09android%3Adrawable%3D%22%40drawable%2Ficon_home_shape_overlay%22%2F%3E%20%0A%09%20*%20%3C%2Flayer-list%3E%0A%09%20*%20icon_home_shape_overlay%E5%A6%82%E4%B8%8B%20%0A%09%20*%20%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%3E%20%0A%09%20*%20%3Cshape%0A%09%20*%20%09%09xmlns%3Aandroid%3D%22http%3A%2F%2Fschemas.android.com%2Fapk%2Fres%2Fandroid%22%3E%20%3Csolid%0A%09%20*%20%09%09android%3Acolor%3D%22%2360000000%22%2F%3E%20%0A%09%20*%20%09%09%3Cstroke%20android%3Awidth%3D%223dp%22%0A%09%20*%20%09%09%09color%3D%22%23ff000000%22%2F%3E%20%0A%09%20*%09%09%3Ccorners%20android%3Aradius%3D%2210dp%22%20%2F%3E%20%0A%09%20*%20%3C%2Fshape%3E%0A%09%20*%20%E6%88%96%E8%80%85%E7%9B%B4%E6%8E%A5%E4%BD%BF%E7%94%A8%E4%B8%80%E7%A7%8D%E6%95%88%E6%9E%9C%0A%09%20*%20%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%20%0A%09%20*%20%3Cshape%0A%09%20*%20%09%09xmlns%3Aandroid%3D%22http%3A%2F%2Fschemas.android.com%2Fapk%2Fres%2Fandroid%22%3E%20%0A%09%20*%20%09%09%3Csolid%0A%09%20*%20%09%09%09%09android%3Acolor%3D%22%2399FFFFFF%22%2F%3E%20%0A%09%20*%20%09%09%3Ccorners%20android%3Aradius%3D%2230px%22%2F%3E%0A%09%20*%09%09%3Cpadding%0A%09%20*%20%09%09%09%09android%3Aleft%3D%220dp%22%20android%3Atop%3D%220dp%22%20android%3Aright%3D%220dp%22%0A%09%20*%09%09%09%09%20android%3Abottom%3D%220dp%22%20%2F%3E%0A%09%20*%20%3C%2Fshape%3E%20%0A%09%20*%20%20%E7%84%B6%E5%90%8E%0A%09%20*%20android%3Abackground%3D%22%40drawable%2Fmy_shape_file%22%0A%09%20*%20%0A%09%20*%20%0A%09%20*%202.%E5%9B%BE%E7%89%87%E6%9C%AC%E8%BA%AB%E5%8A%A0%E4%B8%8A%E5%9C%86%E8%A7%92%20Bitmap%20myCoolBitmap%20%3D%20...%20%3B%20%2F%2F%20%3C--%20Your%20bitmap%20you%20want%20rounded%20%0A%09%20*%20%0A%09%20*%20%09%09int%20w%20%3D%20myCoolBitmap.getWidth()%2C%20h%20%3D%20myCoolBitmap.getHeight()%3B%0A%09%20*%20%09%09Bitmap%20rounder%20%3D%20Bitmap.createBitmap(w%2Ch%2CBitmap.Config.ARGB_8888)%3B%20Canvas%0A%09%20*%20%09%09canvas%20%3D%20new%20Canvas(rounder)%3B%0A%09%20*%20%0A%09%20*%20%09%09Paint%20xferPaint%20%3D%20new%20Paint(Paint.ANTI_ALIAS_FLAG)%3B%0A%09%20*%20%09%09xferPaint.setColor(Color.RED)%3B%0A%09%20*%20%0A%09%20*%20%09%09canvas.drawRoundRect(new%20RectF(0%2C0%2Cw%2Ch)%2C%2020.0f%2C%2020.0f%2C%20xferPaint)%3B%0A%09%20*%20%0A%09%20*%20%09%09xferPaint.setXfermode(new%20PorterDuffXfermode(PorterDuff.Mode.DST_IN))%3B%0A%09%20*%20%09%09canvas.drawBitmap(myCoolBitmap%2C%200%2C0%2C%20null)%3B%20%0A%09%20*%20%09%09canvas.drawBitmap(rounder%2C%200%2C0%2C%20xferPaint)%3B%20%0A%09%20*%20%E6%88%96%E8%80%85%20%0A%09%20*%20public%20static%20Bitmap%20getRoundedCornerBitmap(Bitmap%20bitmap)%20%7B%20%0A%09%20*%20%09%09Bitmap%20output%20%3D%20Bitmap.createBitmap(bitmap.getWidth()%2C%0A%09%20*%20%09%09bitmap.getHeight()%2C%20Config.ARGB_8888)%3B%20%0A%09%20*%20%09%09Canvas%20canvas%20%3D%20newCanvas(output)%3B%0A%09%20*%20%0A%09%20*%20%09%09final%20int%20color%20%3D%200xff424242%3B%20final%20Paint%20paint%20%3D%20new%20Paint()%3B%20final%20Rect%0A%09%20*%09%09rect%20%3D%20new%20Rect(0%2C%200%2C%20bitmap.getWidth()%2C%20bitmap.getHeight())%3B%20final%20RectF%0A%09%20*%20%09%09rectF%20%3D%20new%20RectF(rect)%3B%20final%20float%20roundPx%20%3D%2012%3B%0A%09%20*%20%0A%09%20*%20%09%09paint.setAntiAlias(true)%3B%20canvas.drawARGB(0%2C%200%2C%200%2C%200)%3B%0A%09%20*%20%09%09paint.setColor(color)%3B%20canvas.drawRoundRect(rectF%2C%20roundPx%2C%20roundPx%2Cpaint)%3B%0A%09%20*%20%0A%09%20*%20%09%09paint.setXfermode(new%20PorterDuffXfermode(Mode.SRC_IN))%3B%0A%09%20*%20%09%09canvas.drawBitmap(bitmap%2C%20rect%2C%20rect%2C%20paint)%3B%0A%09%20*%20%0A%09%20*%20%09%09return%20output%3B%20%0A%09%20*%20%7D%0A%09%20*%2F" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="15" width="14">&nbsp;<a href="javascript:void()" title="收藏这段代码" onclick="code_favorites_do_favorite(this);return false;"><img class="star" src="1058517_files/icon_star.png" alt="收藏代码"><img class="spinner" src="1058517_files/spinner.gif" style="display:none"></a></div></div><ol class="dp-j" start="1"><li><span><span class="keyword">class</span><span>&nbsp;RoundImageView&nbsp;</span><span class="keyword">extends</span><span>&nbsp;View&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">private</span><span>&nbsp;Bitmap&nbsp;bitmap;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">int</span><span>&nbsp;bitmapWidth;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">int</span><span>&nbsp;bitmapHeight;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;RoundImageView(Context&nbsp;context)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">super</span><span>(context);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bitmap&nbsp;=&nbsp;BitmapFactory.decodeResource(getResources(),R.drawable.rotate_surfaceview);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bitmapWidth&nbsp;=&nbsp;bitmap.getWidth();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bitmapHeight&nbsp;=&nbsp;bitmap.getHeight();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="annotation">@Override</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">protected</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;onDraw(Canvas&nbsp;canvas)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">super</span><span>.onDraw(canvas);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;第一种方法：</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*Bitmap&nbsp;roundBitmap&nbsp;=&nbsp;Bitmap.createBitmap(bitmapWidth,&nbsp;bitmapHeight,Bitmap.Config.ARGB_8888);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas&nbsp;=&nbsp;new&nbsp;Canvas(roundBitmap);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Paint&nbsp;paint&nbsp;=&nbsp;new&nbsp;Paint(Paint.ANTI_ALIAS_FLAG);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setColor(Color.BLUE);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawRoundRect(new&nbsp;RectF(0,&nbsp;0,&nbsp;bitmapWidth,&nbsp;bitmapHeight),20.0f,&nbsp;20.0f,&nbsp;paint);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setXfermode(new&nbsp;PorterDuffXfermode(PorterDuff.Mode.DST_IN));</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawBitmap(bitmap,&nbsp;0,&nbsp;0,&nbsp;null);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawBitmap(roundBitmap,&nbsp;0,&nbsp;0,&nbsp;paint);*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;第二种方法：</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Paint&nbsp;paint&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Paint(Paint.ANTI_ALIAS_FLAG);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setColor(<span class="number">0xffffffff</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setTextSize(<span class="number">15</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawText(<span class="string">"生成带圆角的图片"</span><span>,&nbsp;</span><span class="number">10</span><span>,&nbsp;</span><span class="number">25</span><span>,&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawBitmap(getRoundedCornerBitmap(bitmap),&nbsp;<span class="number">10</span><span>,&nbsp;</span><span class="number">30</span><span>,&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawText(<span class="string">"生成带倒影的图片"</span><span>,&nbsp;</span><span class="number">170</span><span>,&nbsp;</span><span class="number">160</span><span>,&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawBitmap(createReflectionImageWithOrigin(bitmap),&nbsp;<span class="number">170</span><span>,&nbsp;</span><span class="number">165</span><span>,&nbsp;paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;Bitmap&nbsp;getRoundedCornerBitmap(Bitmap&nbsp;bitmap)&nbsp;{&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;创建一个指定宽度和高度的空位图对象</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Bitmap&nbsp;output&nbsp;=&nbsp;Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),&nbsp;Config.ARGB_8888);&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;用该位图创建画布</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Canvas&nbsp;canvas&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Canvas(output);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;画笔对象</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">final</span><span>&nbsp;Paint&nbsp;paint&nbsp;=&nbsp;</span><span class="keyword">new</span><span>&nbsp;Paint();&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;画笔的颜色</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">final</span><span>&nbsp;</span><span class="keyword">int</span><span>&nbsp;color&nbsp;=&nbsp;</span><span class="number">0xff424242</span><span>;&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;矩形区域对象</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">final</span><span>&nbsp;Rect&nbsp;rect&nbsp;=&nbsp;</span><span class="keyword">new</span><span>&nbsp;Rect(</span><span class="number">0</span><span>,&nbsp;</span><span class="number">0</span><span>,&nbsp;bitmap.getWidth(),&nbsp;bitmap.getHeight());&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;未知</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">final</span><span>&nbsp;RectF&nbsp;rectF&nbsp;=&nbsp;</span><span class="keyword">new</span><span>&nbsp;RectF(rect);&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;拐角的半径</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">final</span><span>&nbsp;</span><span class="keyword">float</span><span>&nbsp;roundPx&nbsp;=&nbsp;</span><span class="number">12</span><span>;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;消除锯齿</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setAntiAlias(<span class="keyword">true</span><span>);&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;画布背景色</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawARGB(<span class="number">0</span><span>,&nbsp;</span><span class="number">0</span><span>,&nbsp;</span><span class="number">0</span><span>,&nbsp;</span><span class="number">0</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;设置画笔颜色</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setColor(color);&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;绘制圆角矩形</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawRoundRect(rectF,&nbsp;roundPx,&nbsp;roundPx,paint);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;未知</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setXfermode(<span class="keyword">new</span><span>&nbsp;PorterDuffXfermode(Mode.SRC_IN));&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;把该图片绘制在该圆角矩形区域中</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawBitmap(bitmap,&nbsp;rect,&nbsp;rect,&nbsp;paint);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;最终在画布上呈现的就是该圆角矩形图片，然后我们返回该Bitmap对象</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">return</span><span>&nbsp;output;&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//获得带倒影的图片方法&nbsp;</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;Bitmap&nbsp;createReflectionImageWithOrigin(Bitmap&nbsp;bitmap){&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;图片与倒影之间的距离间隔</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">final</span><span>&nbsp;</span><span class="keyword">int</span><span>&nbsp;reflectionGap&nbsp;=&nbsp;</span><span class="number">2</span><span>;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;原图的宽度</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">int</span><span>&nbsp;width&nbsp;=&nbsp;bitmap.getWidth();&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;原图的高度</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">int</span><span>&nbsp;height&nbsp;=&nbsp;bitmap.getHeight();&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;图片旋转，缩放等控制对象</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Matrix&nbsp;matrix&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Matrix();&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;缩放（这里pre,set,post三种效果是不一样的，注意区别）</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matrix.preScale(<span class="number">1</span><span>,&nbsp;-</span><span class="number">1</span><span>);&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/**</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set
是直接设置Matrix的值，每次set一次，整个Matrix的数组都会变掉。&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;post
是后乘，当前的矩阵乘以参数给出的矩阵。可以连续多次使用post，</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;来完成所需的整个变换。例如，要将一个图片旋</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
转30度，然后平移到(100,100)的地方，那么可以这样做:&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Matrix&nbsp;m&nbsp;=&nbsp;new&nbsp;Matrix();</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m.postRotate(30);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m.postTranslate(100,&nbsp;100);&nbsp;&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;这样就达到了想要的效果。&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pre
是前乘，参数给出的矩阵乘以当前的矩阵。所以操作是在当前矩阵的最前面发生的。</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;例如上面的例子，如果用pre的话，就要这样:</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Matrix&nbsp;m&nbsp;=&nbsp;new&nbsp;Matrix();</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m.setTranslate(100,&nbsp;100);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m.preRotate(30);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
旋转、缩放和倾斜都可以围绕一个中心点来进行，如果不指定，默认情况下，</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;是围绕(0,0)点来进行。&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;关于缩放：</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scale
的参数是比例。例如，我们缩放为100%，则有一点要注意，如果直接用</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;100/bmp.getWidth()
的话，会得到0，因为是整型相除，所以必须其中有一个是</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;float型的，直接用100f就好&nbsp;。</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
如：matrix.setScale(100f/bmp.getWidth(),&nbsp;100f
/bmp.getHeight());&nbsp;&nbsp;&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;创建一个初始的倒影位图</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Bitmap&nbsp;reflectionImage&nbsp;=&nbsp;Bitmap.createBitmap(bitmap,&nbsp;<span class="number">0</span><span>,&nbsp;height/</span><span class="number">2</span><span>,&nbsp;width,&nbsp;height/</span><span class="number">2</span><span>,&nbsp;matrix,&nbsp;</span><span class="keyword">false</span><span>);&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;新建一个宽度为原图宽度，高度为原图高度的3/2的位图，用于绘制新的位图，即整体的效果图位图对象</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Bitmap&nbsp;bitmapWithReflection&nbsp;=&nbsp;Bitmap.createBitmap(width,&nbsp;(height&nbsp;+&nbsp;height/<span class="number">2</span><span>),&nbsp;Config.ARGB_8888);&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;由该位图对象创建初始画布(规定了画布的宽高)</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Canvas&nbsp;canvas&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Canvas(bitmapWithReflection);&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;在该画布上绘制原图</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawBitmap(bitmap,&nbsp;<span class="number">0</span><span>,&nbsp;</span><span class="number">0</span><span>,&nbsp;</span><span class="keyword">null</span><span>);&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;创建一个画笔</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Paint&nbsp;deafalutPaint&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Paint();&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;绘制一个矩形区域，该矩形区域便是原图和倒影图之间的间隔图</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawRect(<span class="number">0</span><span>,&nbsp;height,width,height&nbsp;+&nbsp;reflectionGap,deafalutPaint);&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;绘制该倒影图于间隔图的下方</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawBitmap(reflectionImage,&nbsp;<span class="number">0</span><span>,&nbsp;height&nbsp;+&nbsp;reflectionGap,&nbsp;</span><span class="keyword">null</span><span>);&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;创建一个画笔</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Paint&nbsp;paint&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Paint();&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;创建一个线性渐变对象</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LinearGradient&nbsp;shader&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;LinearGradient(&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="number">0</span><span>,&nbsp;bitmap.getHeight(),&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="number">0</span><span>,&nbsp;bitmapWithReflection.getHeight()&nbsp;+&nbsp;reflectionGap,&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="number">0x70ffffff</span><span>,&nbsp;</span><span class="number">0x00ffffff</span><span>,&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TileMode.CLAMP&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;把渐变效果应用在画笔上</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setShader(shader);&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;Set&nbsp;the&nbsp;Transfer&nbsp;mode&nbsp;to&nbsp;be&nbsp;porter&nbsp;duff&nbsp;and&nbsp;destination&nbsp;in&nbsp;</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;未知</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setXfermode(<span class="keyword">new</span><span>&nbsp;PorterDuffXfermode(Mode.DST_IN));&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;Draw&nbsp;a&nbsp;rectangle&nbsp;using&nbsp;the&nbsp;paint&nbsp;with&nbsp;our&nbsp;linear&nbsp;gradient&nbsp;</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;绘制出该渐变效果，也就是最终的倒影效果图</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawRect(<span class="number">0</span><span>,&nbsp;height,&nbsp;width,&nbsp;bitmapWithReflection.getHeight()&nbsp;+&nbsp;reflectionGap,&nbsp;paint);&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;返回</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">return</span><span>&nbsp;bitmapWithReflection;&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/**</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;绘制圆角背景以及图片圆角的处理</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.配置文件实现&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;?xml&nbsp;version="1.0"&nbsp;encoding="utf-8"?&gt;&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;layer-list</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xmlns:android="http://schemas.android.com/apk/res/android"&gt;&nbsp;&lt;item</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:drawable="@drawable/icon_home_button_img"/&gt;&nbsp;&lt;item</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:drawable="@drawable/icon_home_shape_overlay"/&gt;&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&lt;/layer-list&gt;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;icon_home_shape_overlay如下&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&lt;?xml&nbsp;version="1.0"&nbsp;encoding="utf-8"?&gt;&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&lt;shape</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xmlns:android="http://schemas.android.com/apk/res/android"&gt;&nbsp;&lt;solid</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:color="#60000000"/&gt;&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;stroke&nbsp;android:width="3dp"</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;color="#ff000000"/&gt;&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;corners&nbsp;android:radius="10dp"&nbsp;/&gt;&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&lt;/shape&gt;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;或者直接使用一种效果</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&lt;?xml&nbsp;version="1.0"&nbsp;encoding="UTF-8"?&gt;&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&lt;shape</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xmlns:android="http://schemas.android.com/apk/res/android"&gt;&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;solid</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:color="#99FFFFFF"/&gt;&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;corners&nbsp;android:radius="30px"/&gt;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;padding</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:left="0dp"&nbsp;android:top="0dp"&nbsp;android:right="0dp"</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:bottom="0dp"&nbsp;/&gt;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&lt;/shape&gt;&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;然后</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;android:background="@drawable/my_shape_file"</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;2.
图片本身加上圆角&nbsp;Bitmap&nbsp;myCoolBitmap&nbsp;=&nbsp;...&nbsp;;&nbsp;//&
nbsp;&lt;--&nbsp;Your&nbsp;bitmap&nbsp;you&nbsp;want&nbsp;rounded&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;w&nbsp;=&nbsp;myCoolBitmap.getWidth(),&nbsp;h&nbsp;=&nbsp;myCoolBitmap.getHeight();</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Bitmap&nbsp;rounder&nbsp;=&nbsp;Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);&nbsp;Canvas</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas&nbsp;=&nbsp;new&nbsp;Canvas(rounder);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Paint&nbsp;xferPaint&nbsp;=&nbsp;new&nbsp;Paint(Paint.ANTI_ALIAS_FLAG);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xferPaint.setColor(Color.RED);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawRoundRect(new&nbsp;RectF(0,0,w,h),&nbsp;20.0f,&nbsp;20.0f,&nbsp;xferPaint);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xferPaint.setXfermode(new&nbsp;PorterDuffXfermode(PorterDuff.Mode.DST_IN));</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawBitmap(myCoolBitmap,&nbsp;0,0,&nbsp;null);&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawBitmap(rounder,&nbsp;0,0,&nbsp;xferPaint);&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;或者&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;public&nbsp;static&nbsp;Bitmap&nbsp;getRoundedCornerBitmap(Bitmap&nbsp;bitmap)&nbsp;{&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Bitmap&nbsp;output&nbsp;=&nbsp;Bitmap.createBitmap(bitmap.getWidth(),</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bitmap.getHeight(),&nbsp;Config.ARGB_8888);&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Canvas&nbsp;canvas&nbsp;=&nbsp;newCanvas(output);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;final&nbsp;int&nbsp;color&nbsp;=&nbsp;0xff424242;&nbsp;final&nbsp;Paint&nbsp;paint&nbsp;=&nbsp;new&nbsp;Paint();&nbsp;final&nbsp;Rect</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rect&nbsp;=&nbsp;new&nbsp;Rect(0,&nbsp;0,&nbsp;bitmap.getWidth(),&nbsp;bitmap.getHeight());&nbsp;final&nbsp;RectF</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rectF&nbsp;=&nbsp;new&nbsp;RectF(rect);&nbsp;final&nbsp;float&nbsp;roundPx&nbsp;=&nbsp;12;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setAntiAlias(true);&nbsp;canvas.drawARGB(0,&nbsp;0,&nbsp;0,&nbsp;0);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setColor(color);&nbsp;canvas.drawRoundRect(rectF,&nbsp;roundPx,&nbsp;roundPx,paint);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paint.setXfermode(new&nbsp;PorterDuffXfermode(Mode.SRC_IN));</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawBitmap(bitmap,&nbsp;rect,&nbsp;rect,&nbsp;paint);</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;output;&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;}</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li></ol></div><pre title="Android 图形用户界面 之 绘图（一）" pre_index="7" source_url="http://emmet1988.iteye.com/blog/1058517" codeable_type="Blog" codeable_id="1058517" style="display: none;" class="java" name="code">class RoundImageView extends View {

		private Bitmap bitmap;
		int bitmapWidth;
		int bitmapHeight;

		public RoundImageView(Context context) {
			super(context);
			bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.rotate_surfaceview);
			bitmapWidth = bitmap.getWidth();
			bitmapHeight = bitmap.getHeight();
		}

		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			// 第一种方法：
			/*Bitmap roundBitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight,Bitmap.Config.ARGB_8888);
			canvas = new Canvas(roundBitmap);
			Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
			paint.setColor(Color.BLUE);
			canvas.drawRoundRect(new RectF(0, 0, bitmapWidth, bitmapHeight),20.0f, 20.0f, paint);
			paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
			canvas.drawBitmap(bitmap, 0, 0, null);
			canvas.drawBitmap(roundBitmap, 0, 0, paint);*/
			// 第二种方法：
			Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
			paint.setColor(0xffffffff);
			paint.setTextSize(15);
			canvas.drawText("生成带圆角的图片", 10, 25, paint);
			canvas.drawBitmap(getRoundedCornerBitmap(bitmap), 10, 30, paint);
			
			canvas.drawText("生成带倒影的图片", 170, 160, paint);
			canvas.drawBitmap(createReflectionImageWithOrigin(bitmap), 170, 165, paint);
			
		}
		
		public Bitmap getRoundedCornerBitmap(Bitmap bitmap) { 
			// 创建一个指定宽度和高度的空位图对象
	  		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), Config.ARGB_8888); 
	 		// 用该位图创建画布
	  		Canvas canvas = new Canvas(output);
	  		// 画笔对象
	  		final Paint paint = new Paint(); 
	  		// 画笔的颜色
	  		final int color = 0xff424242; 
	  		// 矩形区域对象
	  		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
	  		// 未知
	  		final RectF rectF = new RectF(rect); 
	  		// 拐角的半径
	  		final float roundPx = 12;
	  		// 消除锯齿
	  		paint.setAntiAlias(true); 
	  		// 画布背景色
	  		canvas.drawARGB(0, 0, 0, 0);
	  		// 设置画笔颜色
	  		paint.setColor(color); 
	  		// 绘制圆角矩形
	  		canvas.drawRoundRect(rectF, roundPx, roundPx,paint);
	  		// 未知
	  		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
	  		// 把该图片绘制在该圆角矩形区域中
	  		canvas.drawBitmap(bitmap, rect, rect, paint);
	  		// 最终在画布上呈现的就是该圆角矩形图片，然后我们返回该Bitmap对象
			return output; 
		}
		
		//获得带倒影的图片方法 
		public Bitmap createReflectionImageWithOrigin(Bitmap bitmap){ 
			// 图片与倒影之间的距离间隔
			final int reflectionGap = 2;
			// 原图的宽度
			int width = bitmap.getWidth(); 
			// 原图的高度
			int height = bitmap.getHeight(); 
			// 图片旋转，缩放等控制对象
			Matrix matrix = new Matrix(); 
			// 缩放（这里pre,set,post三种效果是不一样的，注意区别）
			matrix.preScale(1, -1); 
			/**
			  	set是直接设置Matrix的值，每次set一次，整个Matrix的数组都会变掉。 
				post是后乘，当前的矩阵乘以参数给出的矩阵。可以连续多次使用post，
				来完成所需的整个变换。例如，要将一个图片旋
				转30度，然后平移到(100,100)的地方，那么可以这样做: 
				Matrix m = new Matrix();
				m.postRotate(30);
				m.postTranslate(100, 100);  
				这样就达到了想要的效果。 
				pre是前乘，参数给出的矩阵乘以当前的矩阵。所以操作是在当前矩阵的最前面发生的。
				例如上面的例子，如果用pre的话，就要这样:
				Matrix m = new Matrix();
				m.setTranslate(100, 100);
				m.preRotate(30);
				旋转、缩放和倾斜都可以围绕一个中心点来进行，如果不指定，默认情况下，
				是围绕(0,0)点来进行。 
				
				关于缩放：
				scale的参数是比例。例如，我们缩放为100%，则有一点要注意，如果直接用
				100/bmp.getWidth()的话，会得到0，因为是整型相除，所以必须其中有一个是
				float型的，直接用100f就好 。
				如：matrix.setScale(100f/bmp.getWidth(), 100f/bmp.getHeight());   
			 */
			// 创建一个初始的倒影位图
			Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height/2, width, height/2, matrix, false); 
			// 新建一个宽度为原图宽度，高度为原图高度的3/2的位图，用于绘制新的位图，即整体的效果图位图对象
			Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/2), Config.ARGB_8888); 
			// 由该位图对象创建初始画布(规定了画布的宽高)
			Canvas canvas = new Canvas(bitmapWithReflection); 
			// 在该画布上绘制原图
			canvas.drawBitmap(bitmap, 0, 0, null); 
			// 创建一个画笔
			Paint deafalutPaint = new Paint(); 
			// 绘制一个矩形区域，该矩形区域便是原图和倒影图之间的间隔图
			canvas.drawRect(0, height,width,height + reflectionGap,deafalutPaint); 
			// 绘制该倒影图于间隔图的下方
			canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null); 
			// 创建一个画笔
			Paint paint = new Paint();
			// 创建一个线性渐变对象
			LinearGradient shader = new LinearGradient(
															0, bitmap.getHeight(), 
															0, bitmapWithReflection.getHeight() + reflectionGap, 
															0x70ffffff, 0x00ffffff, 
															TileMode.CLAMP
													  ); 
			// 把渐变效果应用在画笔上
			paint.setShader(shader); 
			// Set the Transfer mode to be porter duff and destination in 
			// 未知
			paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); 
			// Draw a rectangle using the paint with our linear gradient 
			// 绘制出该渐变效果，也就是最终的倒影效果图
			canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint); 
			// 返回
			return bitmapWithReflection; 
		} 

	}

	/**
	 * 绘制圆角背景以及图片圆角的处理
	   .配置文件实现 
	   &lt;?xml version="1.0" encoding="utf-8"?&gt; 
	   &lt;layer-list
	 * 		xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item
	 * 		android:drawable="@drawable/icon_home_button_img"/&gt; &lt;item
	 * 		android:drawable="@drawable/icon_home_shape_overlay"/&gt; 
	 * &lt;/layer-list&gt;
	 * icon_home_shape_overlay如下 
	 * &lt;?xml version="1.0" encoding="utf-8"?&gt; 
	 * &lt;shape
	 * 		xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;solid
	 * 		android:color="#60000000"/&gt; 
	 * 		&lt;stroke android:width="3dp"
	 * 			color="#ff000000"/&gt; 
	 *		&lt;corners android:radius="10dp" /&gt; 
	 * &lt;/shape&gt;
	 * 或者直接使用一种效果
	 * &lt;?xml version="1.0" encoding="UTF-8"?&gt; 
	 * &lt;shape
	 * 		xmlns:android="http://schemas.android.com/apk/res/android"&gt; 
	 * 		&lt;solid
	 * 				android:color="#99FFFFFF"/&gt; 
	 * 		&lt;corners android:radius="30px"/&gt;
	 *		&lt;padding
	 * 				android:left="0dp" android:top="0dp" android:right="0dp"
	 *				 android:bottom="0dp" /&gt;
	 * &lt;/shape&gt; 
	 *  然后
	 * android:background="@drawable/my_shape_file"
	 * 
	 * 
	 * 2.图片本身加上圆角 Bitmap myCoolBitmap = ... ; // &lt;-- Your bitmap you want rounded 
	 * 
	 * 		int w = myCoolBitmap.getWidth(), h = myCoolBitmap.getHeight();
	 * 		Bitmap rounder = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888); Canvas
	 * 		canvas = new Canvas(rounder);
	 * 
	 * 		Paint xferPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
	 * 		xferPaint.setColor(Color.RED);
	 * 
	 * 		canvas.drawRoundRect(new RectF(0,0,w,h), 20.0f, 20.0f, xferPaint);
	 * 
	 * 		xferPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
	 * 		canvas.drawBitmap(myCoolBitmap, 0,0, null); 
	 * 		canvas.drawBitmap(rounder, 0,0, xferPaint); 
	 * 或者 
	 * public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) { 
	 * 		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
	 * 		bitmap.getHeight(), Config.ARGB_8888); 
	 * 		Canvas canvas = newCanvas(output);
	 * 
	 * 		final int color = 0xff424242; final Paint paint = new Paint(); final Rect
	 *		rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF
	 * 		rectF = new RectF(rect); final float roundPx = 12;
	 * 
	 * 		paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0);
	 * 		paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx,paint);
	 * 
	 * 		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
	 * 		canvas.drawBitmap(bitmap, rect, rect, paint);
	 * 
	 * 		return output; 
	 * }
	 */</pre>
<p>&nbsp;</p>
<p>SurfaceViewDraw.java</p>
<p>&nbsp;</p>
<div id="" class="dp-highlighter"><div class="bar"><div class="tools">Java代码 <embed wmode="transparent" src="1058517_files/clipboard_new.swf" flashvars="clipboard=package%20com.view%3B%0A%0Aimport%20com.test.R%3B%0A%0Aimport%20android.content.Context%3B%0Aimport%20android.graphics.Bitmap%3B%0Aimport%20android.graphics.BitmapFactory%3B%0Aimport%20android.graphics.Canvas%3B%0Aimport%20android.graphics.Matrix%3B%0Aimport%20android.graphics.Paint%3B%0Aimport%20android.graphics.Rect%3B%0Aimport%20android.util.Log%3B%0Aimport%20android.view.SurfaceHolder%3B%0Aimport%20android.view.SurfaceView%3B%0A%09%2F**%0A%09%20*%20http%3A%2F%2Fwallage.blog.163.com%2Fblog%2Fstatic%2F173896242010101232220959%2F%0A%09%20*%20%40author%20emmet1988.iteye.com%0A%09%20*%0A%09%20*%2F%0Apublic%20class%20SurfaceViewDraw%20extends%20SurfaceView%20implements%20Runnable%2CSurfaceHolder.Callback%20%7B%0A%09%0A%09private%20Bitmap%20backgroundBitmap%3B%0A%09private%20Bitmap%20rotateBitmap%3B%0A%09SurfaceHolder%20surfaceHolder%3B%0A%09%0A%09public%20SurfaceViewDraw(Context%20context)%20%7B%0A%09%09super(context)%3B%0A%09%09backgroundBitmap%20%3D%20BitmapFactory.decodeResource(getResources()%2C%20R.drawable.background_surfaceview)%3B%0A%09%09rotateBitmap%20%3D%20BitmapFactory.decodeResource(getResources()%2C%20R.drawable.rotate_surfaceview)%3B%0A%09%09surfaceHolder%20%3D%20this.getHolder()%3B%0A%09%09surfaceHolder.addCallback(this)%3B%09%0A%09%7D%0A%0A%09public%20void%20surfaceCreated(SurfaceHolder%20holder)%20%7B%0A%09%09new%20Thread(this).start()%3B%0A%09%09Log.d(%22surfaceview%22%2C%20%22surfaceCreated%22)%3B%0A%09%7D%0A%0A%09public%20void%20surfaceChanged(SurfaceHolder%20holder%2C%20int%20format%2C%20int%20width%2C%0A%09%09%09int%20height)%20%7B%0A%09%09Log.d(%22surfaceview%22%2C%20%22surfaceChanged%22)%3B%0A%09%7D%0A%0A%09public%20void%20surfaceDestroyed(SurfaceHolder%20holder)%20%7B%0A%09%09Log.d(%22surfaceview%22%2C%20%22surfaceDestroyed%22)%3B%0A%09%7D%0A%0A%09%40Override%0A%09public%20void%20run()%20%7B%0A%09%09Log.d(%22surfaceview%22%2C%20%22run%22)%3B%0A%09%09Canvas%20canvas%20%3D%20null%3B%0A%09%09int%20rotateValue%20%3D%200%3B%2F%2F%E6%97%8B%E8%BD%AC%E8%A7%92%E5%BA%A6%0A%09%09int%20frameCount%20%3D%200%3B%2F%2F%E5%B8%A7%E8%AE%A1%E6%95%B0%E5%99%A8%0A%09%09while%20(!Thread.currentThread().isInterrupted())%20%7B%0A%09%09%09try%20%7B%20%20%20%20%0A%2F%2F%09%09%09%09canvas%20%3D%20surfaceHolder.lockCanvas()%3B%2F%2F%E8%8E%B7%E5%8F%96%E7%94%BB%E5%B8%83%E5%AF%B9%E8%B1%A1(%E8%8E%B7%E5%8F%96%E6%95%B4%E4%B8%AA%E5%B1%8F%E5%B9%95%E7%9A%84%E7%94%BB%E5%B8%83)%0A%09%09%09%09canvas%20%3D%20surfaceHolder.lockCanvas(new%20Rect(10%2C%2010%2C%20240%2C%20250))%3B%2F%2F%E8%8E%B7%E5%8F%96%E6%9F%90%E4%B8%AA%E5%8C%BA%E5%9F%9F%E7%9A%84%E7%94%BB%E5%B8%83%0A%09%09%09%09Paint%20paint%20%3D%20new%20Paint()%3B%0A%09%09%09%09Log.d(%22surfaceview%22%2C%20%22rotateValue%20%22%20%2BrotateValue%2B%22%7CframeCount%20%22%2BframeCount)%3B%0A%09%09%09%09if%20(frameCount%2B%2B%20%3C%202)%20%7B%2F%2F%E4%BB%85%E5%9C%A8%E7%AC%AC%E4%B8%80%E6%AC%A1%E7%BB%98%E5%88%B6%E6%97%B6%E7%BB%98%E5%88%B6%E8%83%8C%E6%99%AF%0A%09%09%09%09%09%2F*%20%0A%09%09%09%09%09%20*%20%E8%BF%99%E9%87%8C%E4%B8%BA%E4%BB%80%E4%B9%88%E8%AE%BE%E7%BD%AE%E6%88%90%EF%BC%9C2%EF%BC%8C%E8%80%8C%E4%B8%8D%E6%98%AF1%EF%BC%8C%E6%98%AF%E7%94%B1%E4%BA%8ESurfaceView%E6%9C%AC%E8%BA%AB%E7%9A%84%E5%8F%8C%E7%BC%93%E5%86%B2%E6%8A%80%E6%9C%AF%E3%80%82%0A%09%09%09%09%09%09%E8%A6%86%E7%9B%96%E5%88%B7%E6%96%B0%E5%85%B6%E5%AE%9E%E5%B0%B1%E6%98%AF%E5%B0%86%E6%AF%8F%E6%AC%A1%E7%9A%84%E6%96%B0%E7%9A%84%E5%9B%BE%E5%BD%A2%E7%BB%98%E5%88%B6%E5%88%B0%E4%B8%8A%E4%B8%80%E5%B8%A7%E5%8E%BB%EF%BC%8C%0A%09%09%09%09%09%09%E6%89%80%E4%BB%A5%E5%A6%82%E6%9E%9C%E5%9B%BE%E5%83%8F%E6%98%AF%E5%8D%8A%E9%80%8F%E6%98%8E%E7%9A%84%EF%BC%8C%E5%B0%B1%E8%A6%81%E8%80%83%E8%99%91%E9%87%8D%E5%A4%8D%E5%8F%A0%E5%8A%A0%E5%AF%BC%E8%87%B4%E7%9A%84%E9%97%AE%E9%A2%98%E4%BA%86%EF%BC%8C%0A%09%09%09%09%09%09%E8%80%8C%E5%A6%82%E6%9E%9C%E6%98%AF%E5%AE%8C%E5%85%A8%E4%B8%8D%E9%80%8F%E6%98%8E%E7%9A%84%E5%9B%BE%E5%BD%A2%E5%88%99%E4%B8%8D%E4%BC%9A%E6%9C%89%E4%BB%BB%E4%BD%95%E9%97%AE%E9%A2%98%E3%80%82%0A%09%09%09%09%09%09%E8%83%8C%E6%99%AF%E4%BC%9A%E5%9C%A8%E8%83%8C%E6%99%AF%E5%9B%BE%E5%92%8C%E9%BB%91%E8%89%B2%E8%83%8C%E6%99%AF%E4%B9%8B%E9%97%B4%E6%9D%A5%E5%9B%9E%E9%97%AA%E3%80%82%0A%09%09%09%09%09%09%E8%BF%99%E4%B8%AA%E9%97%AE%E9%A2%98%E5%85%B6%E5%AE%9E%E6%98%AF%E6%BA%90%E4%BA%8ESurfaceView%E7%9A%84%E5%8F%8C%E7%BC%93%E5%86%B2%E6%9C%BA%E5%88%B6%EF%BC%8C%E6%88%91%E7%90%86%E8%A7%A3%E5%B0%B1%E6%98%AF%E5%AE%83%E4%BC%9A%E7%BC%93%E5%86%B2%0A%09%09%09%09%09%09%E5%89%8D%E4%B8%A4%E5%B8%A7%E7%9A%84%E5%9B%BE%E5%83%8F%E4%BA%A4%E6%9B%BF%E4%BC%A0%E9%80%92%E7%BB%99%E5%90%8E%E9%9D%A2%E7%9A%84%E5%B8%A7%E7%94%A8%E4%BD%9C%E8%A6%86%E7%9B%96%EF%BC%8C%E8%BF%99%E6%A0%B7%E7%94%B1%E4%BA%8E%E6%88%91%E4%BB%AC%E4%BB%85%E5%9C%A8%E7%AC%AC%E4%B8%80%E5%B8%A7%0A%09%09%09%09%09%09%E7%BB%98%E5%88%B6%E4%BA%86%E8%83%8C%E6%99%AF%EF%BC%8C%E7%AC%AC%E4%BA%8C%E5%B8%A7%E5%B0%B1%E6%98%AF%E6%97%A0%E8%83%8C%E6%99%AF%E7%8A%B6%E6%80%81%E4%BA%86%EF%BC%8C%E4%B8%94%E9%80%9A%E8%BF%87%E5%8F%8C%E7%BC%93%E5%86%B2%E6%9C%BA%E5%88%B6%E4%B8%80%E7%9B%B4%E4%BF%9D%E6%8C%81%E4%B8%8B%0A%09%09%09%09%09%09%E6%9D%A5%EF%BC%8C%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95%E5%B0%B1%E6%98%AF%E6%94%B9%E4%B8%BA%E5%9C%A8%E5%89%8D%E4%B8%A4%E5%B8%A7%E9%83%BD%E8%BF%9B%E8%A1%8C%E8%83%8C%E6%99%AF%E7%BB%98%E5%88%B6%E3%80%82%0A%09%09%09%09%09%20*%2F%0A%09%09%09%09%09canvas.drawBitmap(backgroundBitmap%2C%200%2C%200%2C%20paint)%3B%2F%2F%E7%BB%98%E5%88%B6%E8%83%8C%E6%99%AF%0A%09%09%09%09%7D%0A%09%09%09%09%2F%2F%E5%88%9B%E5%BB%BA%E7%9F%A9%E9%98%B5%E4%BB%A5%E6%8E%A7%E5%88%B6%E5%9B%BE%E7%89%87%E7%9A%84%E6%97%8B%E8%BD%AC%E5%92%8C%E5%B9%B3%E7%A7%BB%0A%09%09%09%09Matrix%20matrix%20%3D%20new%20Matrix()%3B%0A%09%09%09%09rotateValue%20%2B%3D%2040%3B%0A%09%09%09%09matrix.setRotate(rotateValue%2C%20rotateBitmap.getWidth()%2F2%2C%20rotateBitmap.getHeight()%2F2)%3B%0A%2F%2F%09%09%09%09matrix.postRotate(rotateValue%2C%20rotateBitmap.getWidth()%2F2%2C%20rotateBitmap.getHeight()%2F2)%3B%0A%2F%2F%09%09%09%09matrix.setTranslate(100%2C%20rotateValue)%3B%0A%09%09%09%09if%20(rotateValue%20%3D%3D%20360)%20%7B%0A%09%09%09%09%09rotateValue%20%3D%200%3B%20%20%0A%09%09%09%09%7D%0A%09%09%09%09matrix.setTranslate(80%2C50)%3B%2F%2F%E8%AE%BE%E7%BD%AE%E5%B7%A6%E8%BE%B9%E8%B7%9D%E5%92%8C%E4%B8%8A%E8%BE%B9%E8%B7%9D%0A%09%09%09%09%2F%2F%E7%BB%98%E5%88%B6%E9%97%AE%E5%8F%B7%0A%09%09%09%09Log.d(%22surfaceview%22%2C%20%22canvas%20%22%2Bcanvas)%3B%0A%09%09%09%09Log.d(%22surfaceview%22%2C%20%22rotateBitmap%20%22%2BrotateBitmap)%3B%0A%09%09%09%09Log.d(%22surfaceview%22%2C%20%22matrix%20%22%2Bmatrix)%3B%0A%09%09%09%09Log.d(%22surfaceview%22%2C%20%22paint%20%22%2Bpaint)%3B%0A%09%09%09%09if%20(canvas%20!%3D%20null)%20%7B%0A%09%09%09%09%09canvas.drawBitmap(rotateBitmap%2C%20matrix%2C%20paint)%3B%0A%09%09%09%09%09%2F%2F%E8%A7%A3%E9%94%81%E7%94%BB%E5%B8%83%EF%BC%8C%E6%8F%90%E4%BA%A4%E7%94%BB%E5%A5%BD%E7%9A%84%E5%9B%BE%E5%83%8F%0A%09%09%09%09%09surfaceHolder.unlockCanvasAndPost(canvas)%3B%0A%09%09%09%09%7D%0A%09%09%09%09Thread.sleep(30)%3B%0A%09%09%09%7D%20catch%20(InterruptedException%20e)%20%7B%0A%09%09%09%09Thread.currentThread().interrupt()%3B%0A%09%09%09%09Log.d(%22surfaceview%22%2C%20%22InterruptedException%22)%3B%0A%09%09%09%7D%20finally%20%7B%0A%09%09%09%09Log.d(%22surfaceview%22%2C%20%22finally%22)%3B%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%0A%7D%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" height="15" width="14">&nbsp;<a href="javascript:void()" title="收藏这段代码" onclick="code_favorites_do_favorite(this);return false;"><img class="star" src="1058517_files/icon_star.png" alt="收藏代码"><img class="spinner" src="1058517_files/spinner.gif" style="display:none"></a></div></div><ol class="dp-j" start="1"><li><span><span class="keyword">package</span><span>&nbsp;com.view;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span><span class="keyword">import</span><span>&nbsp;com.test.R;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span><span class="keyword">import</span><span>&nbsp;android.content.Context;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.Bitmap;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.BitmapFactory;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.Canvas;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.Matrix;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.Paint;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.graphics.Rect;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.util.Log;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.view.SurfaceHolder;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">import</span><span>&nbsp;android.view.SurfaceView;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/**</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;http://wallage.blog.163.com/blog/static/173896242010101232220959/</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@author&nbsp;emmet1988.iteye.com</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">public</span><span>&nbsp;</span><span class="keyword">class</span><span>&nbsp;SurfaceViewDraw&nbsp;</span><span class="keyword">extends</span><span>&nbsp;SurfaceView&nbsp;</span><span class="keyword">implements</span><span>&nbsp;Runnable,SurfaceHolder.Callback&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">private</span><span>&nbsp;Bitmap&nbsp;backgroundBitmap;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">private</span><span>&nbsp;Bitmap&nbsp;rotateBitmap;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;SurfaceHolder&nbsp;surfaceHolder;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;SurfaceViewDraw(Context&nbsp;context)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">super</span><span>(context);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backgroundBitmap&nbsp;=&nbsp;BitmapFactory.decodeResource(getResources(),&nbsp;R.drawable.background_surfaceview);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rotateBitmap&nbsp;=&nbsp;BitmapFactory.decodeResource(getResources(),&nbsp;R.drawable.rotate_surfaceview);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;surfaceHolder&nbsp;=&nbsp;<span class="keyword">this</span><span>.getHolder();&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;surfaceHolder.addCallback(<span class="keyword">this</span><span>);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;surfaceCreated(SurfaceHolder&nbsp;holder)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">new</span><span>&nbsp;Thread(</span><span class="keyword">this</span><span>).start();&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.d(<span class="string">"surfaceview"</span><span>,&nbsp;</span><span class="string">"surfaceCreated"</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;surfaceChanged(SurfaceHolder&nbsp;holder,&nbsp;</span><span class="keyword">int</span><span>&nbsp;format,&nbsp;</span><span class="keyword">int</span><span>&nbsp;width,&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">int</span><span>&nbsp;height)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.d(<span class="string">"surfaceview"</span><span>,&nbsp;</span><span class="string">"surfaceChanged"</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;surfaceDestroyed(SurfaceHolder&nbsp;holder)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.d(<span class="string">"surfaceview"</span><span>,&nbsp;</span><span class="string">"surfaceDestroyed"</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="annotation">@Override</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;run()&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.d(<span class="string">"surfaceview"</span><span>,&nbsp;</span><span class="string">"run"</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Canvas&nbsp;canvas&nbsp;=&nbsp;<span class="keyword">null</span><span>;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">int</span><span>&nbsp;rotateValue&nbsp;=&nbsp;</span><span class="number">0</span><span>;</span><span class="comment">//旋转角度</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">int</span><span>&nbsp;frameCount&nbsp;=&nbsp;</span><span class="number">0</span><span>;</span><span class="comment">//帧计数器</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">while</span><span>&nbsp;(!Thread.currentThread().isInterrupted())&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">try</span><span>&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></li><li><span><span class="comment">//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas&nbsp;=&
nbsp;surfaceHolder.lockCanvas();//获取画布对象(获取整个屏幕的画布)</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas&nbsp;=&nbsp;surfaceHolder.lockCanvas(<span class="keyword">new</span><span>&nbsp;Rect(</span><span class="number">10</span><span>,&nbsp;</span><span class="number">10</span><span>,&nbsp;</span><span class="number">240</span><span>,&nbsp;</span><span class="number">250</span><span>));</span><span class="comment">//获取某个区域的画布</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Paint&nbsp;paint&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Paint();&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.d(<span class="string">"surfaceview"</span><span>,&nbsp;</span><span class="string">"rotateValue&nbsp;"</span><span>&nbsp;+rotateValue+</span><span class="string">"|frameCount&nbsp;"</span><span>+frameCount);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">if</span><span>&nbsp;(frameCount++&nbsp;&lt;&nbsp;</span><span class="number">2</span><span>)&nbsp;{</span><span class="comment">//仅在第一次绘制时绘制背景</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/*&nbsp;</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;
这里为什么设置成＜2，而不是1，是由于SurfaceView本身的双缓冲技术。</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
覆盖刷新其实就是将每次的新的图形绘制到上一帧去，</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
所以如果图像是半透明的，就要考虑重复叠加导致的问题了，</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
而如果是完全不透明的图形则不会有任何问题。</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
背景会在背景图和黑色背景之间来回闪。</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
这个问题其实是源于SurfaceView的双缓冲机制，我理解就是它会缓冲</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
前两帧的图像交替传递给后面的帧用作覆盖，这样由于我们仅在第一帧</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
绘制了背景，第二帧就是无背景状态了，且通过双缓冲机制一直保持下</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
来，解决办法就是改为在前两帧都进行背景绘制。</span>&nbsp;</span></li><li><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawBitmap(backgroundBitmap,&nbsp;<span class="number">0</span><span>,&nbsp;</span><span class="number">0</span><span>,&nbsp;paint);</span><span class="comment">//绘制背景</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//创建矩阵以控制图片的旋转和平移</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Matrix&nbsp;matrix&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Matrix();&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rotateValue&nbsp;+=&nbsp;<span class="number">40</span><span>;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matrix.setRotate(rotateValue,&nbsp;rotateBitmap.getWidth()/<span class="number">2</span><span>,&nbsp;rotateBitmap.getHeight()/</span><span class="number">2</span><span>);&nbsp;&nbsp;</span></span></li><li><span><span class="comment">//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matrix.postRotate(rotateValue,&nbsp;rotateBitmap.getWidth()/2,&nbsp;rotateBitmap.getHeight()/2);</span><span>&nbsp;&nbsp;</span></span></li><li><span><span class="comment">//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matrix.setTranslate(100,&nbsp;rotateValue);</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">if</span><span>&nbsp;(rotateValue&nbsp;==&nbsp;</span><span class="number">360</span><span>)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rotateValue&nbsp;=&nbsp;<span class="number">0</span><span>;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matrix.setTranslate(<span class="number">80</span><span>,</span><span class="number">50</span><span>);</span><span class="comment">//设置左边距和上边距</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//绘制问号</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.d(<span class="string">"surfaceview"</span><span>,&nbsp;</span><span class="string">"canvas&nbsp;"</span><span>+canvas);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.d(<span class="string">"surfaceview"</span><span>,&nbsp;</span><span class="string">"rotateBitmap&nbsp;"</span><span>+rotateBitmap);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.d(<span class="string">"surfaceview"</span><span>,&nbsp;</span><span class="string">"matrix&nbsp;"</span><span>+matrix);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.d(<span class="string">"surfaceview"</span><span>,&nbsp;</span><span class="string">"paint&nbsp;"</span><span>+paint);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">if</span><span>&nbsp;(canvas&nbsp;!=&nbsp;</span><span class="keyword">null</span><span>)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;canvas.drawBitmap(rotateBitmap,&nbsp;matrix,&nbsp;paint);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//解锁画布，提交画好的图像</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;surfaceHolder.unlockCanvasAndPost(canvas);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread.sleep(<span class="number">30</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<span class="keyword">catch</span><span>&nbsp;(InterruptedException&nbsp;e)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread.currentThread().interrupt();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.d(<span class="string">"surfaceview"</span><span>,&nbsp;</span><span class="string">"InterruptedException"</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<span class="keyword">finally</span><span>&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Log.d(<span class="string">"surfaceview"</span><span>,&nbsp;</span><span class="string">"finally"</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;</span></li><li><span>}&nbsp;&nbsp;</span></li></ol></div><pre title="Android 图形用户界面 之 绘图（一）" pre_index="8" source_url="http://emmet1988.iteye.com/blog/1058517" codeable_type="Blog" codeable_id="1058517" style="display: none;" class="java" name="code">package com.view;

import com.test.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
	/**
	 * http://wallage.blog.163.com/blog/static/173896242010101232220959/
	 * @author emmet1988.iteye.com
	 *
	 */
public class SurfaceViewDraw extends SurfaceView implements Runnable,SurfaceHolder.Callback {
	
	private Bitmap backgroundBitmap;
	private Bitmap rotateBitmap;
	SurfaceHolder surfaceHolder;
	
	public SurfaceViewDraw(Context context) {
		super(context);
		backgroundBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.background_surfaceview);
		rotateBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.rotate_surfaceview);
		surfaceHolder = this.getHolder();
		surfaceHolder.addCallback(this);	
	}

	public void surfaceCreated(SurfaceHolder holder) {
		new Thread(this).start();
		Log.d("surfaceview", "surfaceCreated");
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		Log.d("surfaceview", "surfaceChanged");
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		Log.d("surfaceview", "surfaceDestroyed");
	}

	@Override
	public void run() {
		Log.d("surfaceview", "run");
		Canvas canvas = null;
		int rotateValue = 0;//旋转角度
		int frameCount = 0;//帧计数器
		while (!Thread.currentThread().isInterrupted()) {
			try {    
//				canvas = surfaceHolder.lockCanvas();//获取画布对象(获取整个屏幕的画布)
				canvas = surfaceHolder.lockCanvas(new Rect(10, 10, 240, 250));//获取某个区域的画布
				Paint paint = new Paint();
				Log.d("surfaceview", "rotateValue " +rotateValue+"|frameCount "+frameCount);
				if (frameCount++ &lt; 2) {//仅在第一次绘制时绘制背景
					/* 
					 * 这里为什么设置成＜2，而不是1，是由于SurfaceView本身的双缓冲技术。
						覆盖刷新其实就是将每次的新的图形绘制到上一帧去，
						所以如果图像是半透明的，就要考虑重复叠加导致的问题了，
						而如果是完全不透明的图形则不会有任何问题。
						背景会在背景图和黑色背景之间来回闪。
						这个问题其实是源于SurfaceView的双缓冲机制，我理解就是它会缓冲
						前两帧的图像交替传递给后面的帧用作覆盖，这样由于我们仅在第一帧
						绘制了背景，第二帧就是无背景状态了，且通过双缓冲机制一直保持下
						来，解决办法就是改为在前两帧都进行背景绘制。
					 */
					canvas.drawBitmap(backgroundBitmap, 0, 0, paint);//绘制背景
				}
				//创建矩阵以控制图片的旋转和平移
				Matrix matrix = new Matrix();
				rotateValue += 40;
				matrix.setRotate(rotateValue, rotateBitmap.getWidth()/2, rotateBitmap.getHeight()/2);
//				matrix.postRotate(rotateValue, rotateBitmap.getWidth()/2, rotateBitmap.getHeight()/2);
//				matrix.setTranslate(100, rotateValue);
				if (rotateValue == 360) {
					rotateValue = 0;  
				}
				matrix.setTranslate(80,50);//设置左边距和上边距
				//绘制问号
				Log.d("surfaceview", "canvas "+canvas);
				Log.d("surfaceview", "rotateBitmap "+rotateBitmap);
				Log.d("surfaceview", "matrix "+matrix);
				Log.d("surfaceview", "paint "+paint);
				if (canvas != null) {
					canvas.drawBitmap(rotateBitmap, matrix, paint);
					//解锁画布，提交画好的图像
					surfaceHolder.unlockCanvasAndPost(canvas);
				}
				Thread.sleep(30);
			} catch (InterruptedException e) {
				Thread.currentThread().interrupt();
				Log.d("surfaceview", "InterruptedException");
			} finally {
				Log.d("surfaceview", "finally");
			}
		}
	}

}
</pre>
<p>&nbsp;</p>
  </div>

  


  <iframe src="1058517_files/794.htm" frameborder="0" height="60" scrolling="no" width="468"></iframe>
  
  <div id="bottoms" class="clearfix">
    
    <div id="share_weibo">分享到：
      <a data-type="sina" href="javascript:;" title="分享到新浪微博"><img src="1058517_files/sina.jpg"></a>
      <a data-type="qq" href="javascript:;" title="分享到腾讯微博"><img src="1058517_files/tec.jpg"></a>
    </div>
  </div>

  <div class="blog_nav">
    <div class="pre_next">
      <a href="http://emmet1988.iteye.com/blog/1058526" class="next" title="Android 图形用户界面 之 绘图（二） Paint类 主要方法介绍">Android 图形用户界面 之 绘图（二） Pai ...</a>
      |
      <a href="http://emmet1988.iteye.com/blog/1058499" class="pre" title="《Ophone应用开发权威指南》学习：Android 图形用户界面 之 动画（一）">《Ophone应用开发权威指南》学习：Android  ...</a>
    </div>
  </div>
  <div class="blog_bottom">
    <ul>
      <li>2011-05-25 17:59</li>
      <li>浏览 8166</li>
      <li><a href="#comments">评论(5)</a></li>
      
      
      <li>分类:<a href="http://www.iteye.com/blogs/category/mobile">移动开发</a></li>      
      <li class="last"><a href="http://www.iteye.com/wiki/blog/1058517" target="_blank" class="more">相关推荐</a></li>
    </ul>
  </div>

  <div class="blog_comment">
    <h5>评论</h5>
    <a id="comments" name="comments"></a>
    <div id="bc2250371">
  <div class="comment_title">
    5 楼
    <a href="http://867797806-qq-com.iteye.com/" target="_blank" title="梦林忆雨">梦林忆雨</a>
    2012-03-30&nbsp;&nbsp;
    
    
  </div>
  <div class="comment_content"><img src="1058517_files/icon_smile.gif"> 谢谢，学习了啊！</div>
</div>

<div id="bc2244630">
  <div class="comment_title">
    4 楼
    <a href="http://caofeijuan-163-com.iteye.com/" target="_blank" title="力争向日葵">力争向日葵</a>
    2012-02-24&nbsp;&nbsp;
    
    
  </div>
  <div class="comment_content">谢谢呀！很好的学下了</div>
</div>

<div id="bc2219796">
  <div class="comment_title">
    3 楼
    <a href="http://emmet1988.iteye.com/" target="_blank" title="ldci3gandroid">ldci3gandroid</a>
    2011-09-28&nbsp;&nbsp;
    
    
  </div>
  <div class="comment_content"><div class="quote_title">不夜的星辰 写道</div><div class="quote_div">请问下你这种版述效果如何设置？</div><br>不好意思啊，没明白你说的是什么意思？</div>
</div>

<div id="bc2218258">
  <div class="comment_title">
    2 楼
    <a href="http://shufeipenglove-163-com.iteye.com/" target="_blank" title="不夜的星辰">不夜的星辰</a>
    2011-09-21&nbsp;&nbsp;
    
    
  </div>
  <div class="comment_content">请问下你这种版述效果如何设置？</div>
</div>

<div id="bc2205841">
  <div class="comment_title">
    1 楼
    <a href="http://jackdg2010.iteye.com/" target="_blank" title="JACKDG2010">JACKDG2010</a>
    2011-07-20&nbsp;&nbsp;
    
    
  </div>
  <div class="comment_content"><img src="1058517_files/icon_idea.gif">&nbsp;<img src="1058517_files/icon_wink.gif">&nbsp;<img src="1058517_files/icon_wink.gif">&nbsp;<img src="1058517_files/icon_wink.gif"> </div>
</div>


    
    
  </div>

  <div class="blog_comment">
    <h5>发表评论</h5>
            <p style="text-align:center; margin-top:30px;margin-bottom:0px;"><a href="http://emmet1988.iteye.com/login" style="background-color:white;"> <img src="1058517_files/login_icon.png" style="vertical-align:middle; margin-right: 10px;"></a><a href="http://emmet1988.iteye.com/login">  您还没有登录,请您登录后再发表评论 </a></p>
      </div>
</div>


<script type="text/javascript">
  dp.SyntaxHighlighter.HighlightAll('code', true, true);

  $$('#main .blog_content pre[name=code]').each(function(pre, index){ // blog content
    var post_id = 1058517;
    var location = window.location;
    source_url = location.protocol + "//" + location.host + location.pathname + location.search;
    pre.writeAttribute('codeable_id', post_id);
    pre.writeAttribute('codeable_type', "Blog");
    pre.writeAttribute('source_url', source_url);
    pre.writeAttribute('pre_index', index);
    pre.writeAttribute('title', 'Android 图形用户界面 之 绘图（一）');
  });

  fix_image_size($$('div.blog_content img'), 700);

  function processComment() {
    $$('#main .blog_comment > div').each(function(comment){// comment
      var post_id = comment.id.substr(2);
      $$("#"+comment.id+" pre[name=code]").each(function(pre, index){
        var location = window.location;
        source_url = location.protocol + "//" + location.host + location.pathname + location.search;
        source_url += "#" + comment.id;
        pre.writeAttribute('codeable_id', post_id);
        pre.writeAttribute('codeable_type', "BlogComment");
        pre.writeAttribute('source_url', source_url);
        pre.writeAttribute('pre_index', index);
        pre.writeAttribute('title', 'Android 图形用户界面 之 绘图（一）');
      });
    });
  }

  function quote_comment(id) {
    new Ajax.Request('/editor/quote', {
      parameters: {'id':id, 'type':'BlogComment'},
      onSuccess:function(response){editor.bbcode_editor.textarea.insertAfterSelection(response.responseText);
        Element.scrollTo(editor.bbcode_editor.textarea.element);}
    });
  }

  code_favorites_init();
  processComment();
  new WeiboShare({share_buttons: $('share_weibo'), img_scope: $('blog_content')});
</script>




        </div>

        <div id="local">
          <div class="local_top"></div>
          <div id="blog_owner">
  <div id="blog_owner_logo"><a href="http://emmet1988.iteye.com/"><img alt="ldci3gandroid的博客" class="logo" src="1058517_files/65a14334-6551-3b1f-8c6f-9092b7c4cae7.jpg" title="ldci3gandroid的博客: 蚂蚁乐园"></a></div>
  <div id="blog_owner_name">ldci3gandroid</div>
</div>

          <div id="blog_actions">
            <ul>
              <li>浏览: 93341 次</li>
              <li>性别: <img alt="Icon_minigender_1" src="1058517_files/icon_minigender_1.gif" title="男"></li>
              <li>来自: 北京</li>
              <li><img src="1058517_files/offline.gif"></li>
              
            </ul>
          </div>
          <div id="user_visits" class="clearfix">
            <h5>最近访客 <span style="font-weight:normal;font-size:12px;padding-left:30px;"><a href="http://emmet1988.iteye.com/blog/user_visits">更多访客&gt;&gt;</a></span></h5>
            
              <div class="user_visit">
                <div class="logo"><a href="http://kevin-csshi.iteye.com/" target="_blank"><img alt="kevin_csshi的博客" class="logo" src="1058517_files/user-logo-thumb.gif" title="kevin_csshi的博客: "></a></div>
                <div class="left"><a href="http://kevin-csshi.iteye.com/" target="_blank" title="kevin_csshi">kevin_csshi</a></div>
              </div>
            
              <div class="user_visit">
                <div class="logo"><a href="http://dylinshi126.iteye.com/" target="_blank"><img alt="dylinshi126的博客" class="logo" src="1058517_files/user-logo-thumb.gif" title="dylinshi126的博客: "></a></div>
                <div class="left"><a href="http://dylinshi126.iteye.com/" target="_blank" title="dylinshi126">dylinshi126</a></div>
              </div>
            
              <div class="user_visit">
                <div class="logo"><a href="http://bigpiglet-zju.iteye.com/" target="_blank"><img alt="bigpiglet_zju的博客" class="logo" src="1058517_files/user-logo-thumb.gif" title="bigpiglet_zju的博客: "></a></div>
                <div class="left"><a href="http://bigpiglet-zju.iteye.com/" target="_blank" title="bigpiglet_zju">bigpiglet_zju</a></div>
              </div>
            
              <div class="user_visit">
                <div class="logo"><a href="http://201111091017.iteye.com/" target="_blank"><img alt="939808496的博客" class="logo" src="1058517_files/user-logo-thumb.gif" title="939808496的博客: "></a></div>
                <div class="left"><a href="http://201111091017.iteye.com/" target="_blank" title="939808496">939808496</a></div>
              </div>
            
          </div>

          

                      <div id="blog_menu">
              <h5>文章分类</h5>
              <ul>
                <li><a href="http://emmet1988.iteye.com/">全部博客 (88)</a></li>
                
                  <li><a href="http://emmet1988.iteye.com/category/156074">Android入门-初级 (10)</a></li>
                
                  <li><a href="http://emmet1988.iteye.com/category/156075">Android入门-中级 (31)</a></li>
                
                  <li><a href="http://emmet1988.iteye.com/category/156076">Android入门-高级 (5)</a></li>
                
                  <li><a href="http://emmet1988.iteye.com/category/156077">Android进阶-初级 (2)</a></li>
                
                  <li><a href="http://emmet1988.iteye.com/category/156078">Android进阶-中级 (8)</a></li>
                
                  <li><a href="http://emmet1988.iteye.com/category/156079">Android进阶-高级 (6)</a></li>
                
                  <li><a href="http://emmet1988.iteye.com/category/156080">Java入门-初级 (2)</a></li>
                
                  <li><a href="http://emmet1988.iteye.com/category/156081">Java入门-中级 (3)</a></li>
                
                  <li><a href="http://emmet1988.iteye.com/category/156082">Java入门-高级 (0)</a></li>
                
                  <li><a href="http://emmet1988.iteye.com/category/156083">Java进阶-初级 (0)</a></li>
                
                  <li><a href="http://emmet1988.iteye.com/category/156084">Java进阶-中级 (1)</a></li>
                
                  <li><a href="http://emmet1988.iteye.com/category/156085">Java进阶-高级 (2)</a></li>
                
                  <li><a href="http://emmet1988.iteye.com/category/157490">代码仓库之Android (1)</a></li>
                
              </ul>
            </div>
            <div id="month_blogs">
              <h5>社区版块</h5>
              <ul>
                <li><a href="http://emmet1988.iteye.com/blog/news">我的资讯</a> (0)</li>
                <li>
                  <a href="http://emmet1988.iteye.com/blog/post">我的论坛</a> (3)
                </li>
                <li><a href="http://emmet1988.iteye.com/blog/answered_problems">我的问答</a> (0)</li>
              </ul>
            </div>
            <div id="month_blogs">
              <h5>存档分类</h5>
              <ul>
                
                  <li><a href="http://emmet1988.iteye.com/blog/monthblog/2012-04">2012-04</a> (1)</li>
                
                  <li><a href="http://emmet1988.iteye.com/blog/monthblog/2012-02">2012-02</a> (1)</li>
                
                  <li><a href="http://emmet1988.iteye.com/blog/monthblog/2011-12">2011-12</a> (4)</li>
                
                <li><a href="http://emmet1988.iteye.com/blog/monthblog_more">更多存档...</a></li>
              </ul>
            </div>
            
            

            <div id="guest_books">
              <h5>最新评论</h5>
              <ul>
                
                <li>
                  <a href="http://anlins.iteye.com/" target="_blank" title="anlins">anlins</a>： 
                  还有个这个方法有个缺点，就是当listview中的view空间 ...<br>
                  <a href="http://emmet1988.iteye.com/blog/1097443#bc2313495">Android学习 之 问题&amp;解答 ScrollView中嵌套ListView时显示不全的简便解决方案 </a>
                </li>
                
                <li>
                  <a href="http://anlins.iteye.com/" target="_blank" title="anlins">anlins</a>： 
                  linearlayout重写了onmeasure方法，其他的布 ...<br>
                  <a href="http://emmet1988.iteye.com/blog/1097443#bc2313494">Android学习 之 问题&amp;解答 ScrollView中嵌套ListView时显示不全的简便解决方案 </a>
                </li>
                
                <li>
                  <a href="http://376657368.iteye.com/" target="_blank" title="一EE">一EE</a>： 
                  对于方法一：A、屏幕控制需要对应的权限permission声明 ...<br>
                  <a href="http://emmet1988.iteye.com/blog/1058529#bc2308047">Android 系统设置 之 保持手机屏幕常亮的方法</a>
                </li>
                
                <li>
                  <a href="http://zhangshaoru.iteye.com/" target="_blank" title="zhangshaoru">zhangshaoru</a>： 
                  “与方法不同的是，对象的属性则不具备多态性。通过引用变量来访问 ...<br>
                  <a href="http://emmet1988.iteye.com/blog/1064613#bc2255291">Java学习 之 编译时类型和运行时类型</a>
                </li>
                
                <li>
                  <a href="http://yahier.iteye.com/" target="_blank" title="yahier">yahier</a>： 
                  learner576539763 写道真的是空指针。。。。。  ...<br>
                  <a href="http://emmet1988.iteye.com/blog/1097443#bc2251340">Android学习 之 问题&amp;解答 ScrollView中嵌套ListView时显示不全的简便解决方案 </a>
                </li>
                
              </ul>
            </div>

            <div class="local_bottom"></div>
          
        </div>
      </div>

      <div id="footer" class="clearfix">
        <div id="copyright">
          <hr>
          声明：ITeye文章版权属于作者，受法律保护。没有作者书面许可不得转载。若作者同意转载，必须以超链接形式标明文章原始出处和作者。<br>
          © 2003-2012 ITeye.com.   All rights reserved.  [ 京ICP证110151号  京公网安备110105010620 ]
        </div>
      </div>
    </div>
    <script type="text/javascript">
  document.write("<img src='http://stat.iteye.com/?url="+ encodeURIComponent(document.location.href) + "&referrer=" + encodeURIComponent(document.referrer) + "&user_id=' width='0' height='0' />");
</script><img src="1058517_files/a.gif" height="0" width="0">

    
    
  

</body></html>